Android 设置可见性不起作用

标签 android xml visibility

我的 LinearLayout 中有一个 TextView 和一个 Image 如果电话值通过 Intent 传递给 Activity ,那么我将包含文本和图像的布局的可见性设置为可见。

在我的代码中,布局显示,但文本和图像没有显示,即使它们在使布局可见的同一检查中设置为可见。我已经添加了我的 xml 和在我的代码中设置可见性的 Java 代码。

if(extras.getString("phone") != null){
    Log.d("PHONE VISIBLE", "phone made clayout visible");
    contactsLayout.setVisibility(View.VISIBLE);
    phoneLayout.setVisibility(View.VISIBLE);
    mPhone = extras.getString("phone");
    Log.d("places got phone", mPhone.toString());
    phone.setText(mPhone);
}

这是我的xml代码

<LinearLayout
    android:id="@+id/contactsLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_below="@+id/summary"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/contactsLabel"
        android:background="@color/gray"
        android:paddingLeft="16dp"
        android:text="contact"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phoneLayout"
        android:orientation="horizontal"
        android:visibility="gone">

        <ImageView
            android:id="@+id/phoneImage"
            android:src="@drawable/icon_phone"
            android:layout_width="60dp"
            android:layout_height="60dp"/>

        <TextView
            android:id="@+id/phoneNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="16dp"
            tools:text="phoneNumber"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/emailLayout"
        android:paddingLeft="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone">

        <ImageView
            android:id="@+id/emailImage"
            android:src="@drawable/icon_email"
            android:layout_width="60dp"
            android:layout_height="60dp" />

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            tools:text="email"/>
    </LinearLayout>
</LinearLayout>

最佳答案

正如迈克所说

按如下方式更改 contactsLabel:

TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/contactsLabel"
        android:background="@color/gray"
        android:paddingLeft="16dp"
        android:text="contact"/>

关于Android 设置可见性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33075668/

相关文章:

c# - 如何使用 LINQ to XML 将多级 xml 转换为对象?

java - JAXB : unmarshalexception - with linked exception: [java. io.IOException:流已关闭]

java - 折叠不可见的 Java Swing 组件的最佳方法

html - magento CE 中的 css 可见性问题

Android 上下文实践

java - 使用 Firebase Messaging 实现通知时出现空指针异常

java - 如何使用sqlite数据库进行搜索查询

xml - 是否有任何gradle插件来验证XML?

Android Intent 子类化

c# - 添加新引用后如何在工具箱中查看组件?