Кнопка «Изображение» не отображается в программе «Создать» и не активируется - PullRequest
1 голос
/ 07 июля 2019

Я просто хочу добавить метод onClick в мою кнопку Image, но кнопка image просто не видна, но в моем макете. Моя кнопка почему-то была видна, но мой метод onclick не работал. По какой-то причине кнопка с изображением видна в моем макете, но опять же не при запуске приложения.

MainActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Toast.makeText(this, "Welcome to Basic IPTV !", Toast.LENGTH_SHORT).show();

        final Toolbar toolbar = findViewById(R.id.main_toolbar);
        setSupportActionBar(toolbar);




       isPreparing = false;
       pauseisclicked = true;
       imgbuttonisclicked = false;
        isUp = false;
        IsDisabled = false;

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)

                != PackageManager.PERMISSION_GRANTED){
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
        }






        //For now we just picked an arbitrary item to play


        imgbutton = (ImageButton) findViewById(R.id.imageButton7);
        txt = (TextView) findViewById(R.id.textView);
        mainlist = (ListView) findViewById(R.id.mainlistview);
        noresults = (TextView) findViewById(R.id.noresults);

        noresults.setVisibility(View.INVISIBLE);

        imgbutton.setVisibility(View.VISIBLE);



        imgbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new MaterialFilePicker()
                        .withActivity(MainActivity.this)
                        .withRequestCode(1000)
                        .withFilter(Pattern.compile(".*\\.m3u$")) // Filtering files and directories by file name using regexp

                        .withHiddenFiles(true) // Show hidden files and folders
                        .start();
            }


        });


    }

макет, но текст:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">


    <ImageButton
        android:id="@+id/imageButton7"
        android:layout_width="80dp"
        android:layout_height="0dp"
        android:layout_marginStart="100dp"
        android:layout_marginEnd="102dp"
        android:layout_marginBottom="319dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/noresults"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout4"
        app:srcCompat="@drawable/ic_add" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="338dp"
        android:layout_height="99dp"
        android:layout_marginTop="176dp"
        android:layout_marginEnd="8dp"
        android:text="First, add a m3u playlist from your M3U-Subscribtion by clicking the button below or click on the three dots and then on open m3u..."
        app:layout_constraintEnd_toEndOf="@+id/mainlistview"
        app:layout_constraintHorizontal_bias="0.723"
        app:layout_constraintStart_toStartOf="@+id/mainlistview"
        app:layout_constraintTop_toTopOf="@+id/mainlistview" />


    <ListView
        android:id="@+id/mainlistview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="right"
        android:textSize="2dp"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout4">

    </ListView>

    <TextView
        android:id="@+id/noresults"
        android:layout_width="248dp"
        android:layout_height="51dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="104dp"
        android:layout_marginEnd="8dp"
        android:text="No channel found"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="@+id/mainlistview"
        app:layout_constraintTop_toTopOf="@+id/mainlistview" />

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="278dp"
        app:layout_constraintBottom_toTopOf="@+id/imageButton7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/Theme.AppCompat" />
    </com.google.android.material.appbar.AppBarLayout>

Это мой макет:

layout

Здесь: скриншот моего устройства (оно реально) my device

Ответы [ 2 ]

0 голосов
/ 07 июля 2019

измените свою кнопку изображения xml:

   <ImageButton
    android:id="@+id/imageButton7"
    android:layout_width="80dp"
    android:layout_height="0dp"
    android:layout_marginStart="100dp"
    android:layout_marginEnd="102dp"
    android:layout_marginBottom="319dp"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/appBarLayout4"
    app:srcCompat="@drawable/ic_add" />
0 голосов
/ 07 июля 2019

У вас очень большой android:layout_marginBottom="319dp". Если вы запустите приложение на меньшем устройстве, ваш imageButton может быть расположен за пределами экрана.

В целом, код вашего макета выглядит так, как будто его можно улучшить. Если вы новичок в разработке приложений для Android, я предлагаю вам использовать LinearLayout, потому что они проще в использовании. Или посмотрите несколько уроков о ConstraintLayout и о том, как его использовать.

...