微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

想在android应用程序的运行时期间使一些视图不可见

这是我的主要.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dip"
    >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"

android:textColor="@color/mbackground1"
android:gravity="center_horizontal"
android:text="@string/decode_label"
android:padding="5dip" 
/>

<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@color/mbackground2" 
android:textColor="@color/mytextcolor" 
android:padding="5dip"
/>


 <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label"
android:gravity="center_horizontal"
android:textColor="@color/mytextcolor"
android:padding="5dip"
/>

<Button 
android:id="@+id/webbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/web_button"
android:textColor="@color/mytextcolor"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label1"
android:gravity="center_horizontal"
android:textColor="@color/mytextcolor"
android:padding="5dip"
/>

<Button 
android:id="@+id/callbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/call_button"
android:textColor="@color/mytextcolor"
/>
<TextView

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label2"
android:gravity="center_horizontal"
android:textColor="@color/mytextcolor"
android:padding="5dip"
/>

<Button 
android:id="@+id/emailbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sendemail_button"
android:textColor="@color/mytextcolor"
/>

</LinearLayout>

我希望基于运行时的输出,它应该只显示一个文本视图和对应于该输出的按钮.我在main.xml文件中定义布局,我也在这个领域.

有谁有想法吗.
提前致谢

解决方法

我假设你知道如何获得你定义的视图的引用,例如:
Button button = (Button)findViewById(R.id.emailbutton)

您需要为代码中要使用的每个视图定义一个id,就像您对email按钮所做的那样:

android:id="@+id/emailbutton"

要设置您调用的视图的可见性,请执行以下操作:

button.setVisibility(View.GONE);

您可以选择将可见性设置为INVISIBLE和VISIBLE.
然后,您可以随意使用可见性.
INVISIBLE和GONE之间的区别在于GONE完全从布局中移除视图,而INVISIBLE“保存”此视图所占用的空间.

您可以在API示例中看到.

原文地址:https://www.jb51.cc/android/316966.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐