想在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示例中看到.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...