图片按钮在Oncreate中不可见并且不可点击

时间:2019-07-07 12:07:16

标签: android android-layout visibility imagebutton

我只想在Imagebutton中添加onClick方法,但是imagebutton只是不可见,而是在我的布局中。我的按钮以某种方式可见,但是我的onclick方法不起作用。由于某些原因,imagebutton在我的布局中可见,但是在我启动应用程序时却不可见。

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 :(得分:0)

您的android:layout_marginBottom="319dp"很大。如果您在较小的设备上运行该应用,则imageButton可能会位于屏幕之外。

总体而言,布局的代码看起来可以改进。如果您不熟悉Android应用程序开发,建议您使用LinearLayout,因为它们更易于使用。或观看有关ConstraintLayout及其使用方法的一些教程。

答案 1 :(得分:0)

更改您的imagebutton 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" />