【问题标题】:Imagebutton is not visible in Oncreate and not clickableImagebutton 在 Oncreate 中不可见且不可点击
【发布时间】:2019-11-17 04:58:09
【问题描述】:

我只想在我的 Imagebutton 中添加一个 onClick 方法,但 imagebutton 只是不可见,而是在我的布局中。我的按钮以某种方式可见,但我的 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>

这是我的布局:

这里:我的设备的屏幕截图(它是真实的)

【问题讨论】:

  • 你的布局太奇怪了,问题可能与你手机的尺寸有关,因为 Views 的位置似乎是以绝对的方式设置的,而不是受限制的,因为它会在约束布局。因此,例如,您不限制按钮在 textView 下方水平和垂直居中是有原因的?
  • 尝试为 imagebutton 提供固定高度和黑色背景色,以查看它在视图中实际存在的位置,正如 Luca 所说,您需要确认约束是正确的并且项目没有挂起跨度>

标签: android android-layout visibility imagebutton


【解决方案1】:

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

总体而言,您的布局代码看起来可以改进。如果您是 Android 应用程序开发的新手,我建议您使用 LinearLayout,因为它们更易于使用。或者观看一些关于ConstraintLayout 以及如何使用它的教程。

【讨论】:

  • 好的,我想我要使用 LinearLayout。
【解决方案2】:

更改您的图像按钮 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" />

【讨论】:

    猜你喜欢
    • 2015-05-04
    • 2015-09-06
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2018-01-05
    相关资源
    最近更新 更多