【问题标题】:Android Linear Layout is not visibleAndroid 线性布局不可见
【发布时间】:2016-08-12 08:10:56
【问题描述】:

我有一个问题,我想在检查条件后在父级中显示线性布局。我想在父布局的正文中显示线性布局android:id="@+id/linearIndex"。代码在这里。

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    android:id="@+id/lstep3">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone"
    android:id="@+id/linearIndex"> //THE LAYOUT I WANT TO SHOW
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textStyle="bold"
        android:text="Jumlah Kasus Tambahan di Lokasi PE"
        android:textColor="@color/teal"
        />
    <!-- daftar gejala yang dimasukkan-->
    <ListView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:id="@+id/listvievadvance2"
        android:layout_marginTop="10dp"></ListView>

    <!-- form gejala-->
    <!-- form data gejala-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="15dp"
        android:background="@color/accent_501">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pilih Lokasi"
            android:textStyle="bold"/>
        <Spinner
            android:id="@+id/lokasiPilih"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            />

        <TextView
            android:id="@+id/txtLocLain"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lokasi lain"
            android:textStyle="bold"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_marginTop="7dp"
            android:hint="Lokasi lain"
            android:layout_height="wrap_content"
            android:id="@+id/lokasiLain"
            android:theme="@style/TextLabel"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Tanggal PE"
            android:textStyle="bold"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_marginTop="7dp"
            android:hint="Tanggal PE"
            android:layout_height="wrap_content"
            android:id="@+id/tglPE"
            android:editable="false"
            android:theme="@style/TextLabel"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Jumlah Kasus tambahan"
            android:textStyle="bold"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_marginTop="7dp"
            android:hint="Masukan jumlah kasus"
            android:layout_height="wrap_content"
            android:id="@+id/jmlKasusTambah"
            android:theme="@style/TextLabel"
            />

        <Button
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/btambahLokasi"
            android:text="Tambah"
            android:textColor="#fff"
            android:layout_marginTop="10dp"
            android:background="@color/teal"
            />

    </LinearLayout>
</LinearLayout>

还有java文件

显示布局可见性的函数

 private void checkIndexOrNot(){

    Log.i("Value :", String.valueOf(campak.getSurveilans().getStatusKasus()));

    if(Integer.parseInt(campak.getSurveilans().getStatusKasus()) == 1) {
        linearIndex.setVisibility(View.VISIBLE);
    } else {
        linearIndex.setVisibility(View.GONE);
    }

}

我知道它看起来没有问题,但是显示布局可见性的功能不起作用。检查条件后布局不显示。从数据库检查的条件逻辑工作正常,没有错误。 我将 checkIndexOrNot() 放在 onCreate() 函数中。

提前谢谢你。

【问题讨论】:

  • 因为 id lstep3 的根 LinearLayout 总是消失了。删除lstep3 中的android:visibility="gone",你会看到你的LinearLayout
  • 是的,就是这样。它的父级是gone,所以只需删除它。你,伙计,应该写下答案并让其接受@PhanVanLinh。
  • 非常感谢,我接受你的回答@PhanVanLinh

标签: java android xml android-layout android-linearlayout


【解决方案1】:

您的linearIndex 的父布局具有gone 作为默认属性,这意味着它将不可见。要显示子视图,父布局必须可见。因此,要么以编程方式将两个布局都设置为可见,要么只是从 lstep3 中删除 visibility="gone"

【讨论】:

  • 是的,你是对的,我问我的朋友,他的回答和你的一样。那么,如果我在 java GONE 中设置 visibilty lstep3 但在 xml 中删除 visibility="gone" 可以吗?所以,我在java中设置了布局。
  • 好吧,你可以这样做,但如果你将 linearIndex 的可见性设置为可见,你还必须将 lstep3 的可见性设置为可见。
  • 好的,我试试。不管怎样,谢谢你! @babadaba
  • 真的很感谢你,它正在工作。非常感谢! @babadaba
【解决方案2】:

只需从根布局中删除 android:visibility="gone"

改变这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    android:id="@+id/lstep3">

到这里:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lstep3">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    相关资源
    最近更新 更多