1

我正在开发一个 Android 应用程序,但我还是很新。我想要一个按钮,当你按下那个按钮时,会出现一些 TextViews 和 Buttons。所以我有一个主要的线性布局,然后是另一个嵌套在里面的线性布局,其中包含我想要隐藏的东西。我将嵌套线性布局设置为 android:visibility="gone"。

我遇到的问题是它只显示隐藏线性布局中的第一个项目,而不是所有项目。我试图让它出现的方式是

    vgAddView = (ViewGroup)findViewById(R.id.add_details);

    btnAche.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            vgAddView.setVisibility(0);
        }
    });

我的 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"
    >
<Button 
    android:text="@string/but_stomach_ache" 
    android:id="@+id/but_stomach_ache" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
</Button>
<Button 
    android:text="@string/but_food" 
    android:id="@+id/but_food" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
</Button>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/add_details"
    android:visibility="gone">
    <TextView
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/when_happen">
        </TextView>
    <Button 
        android:text="@string/happen_now" 
        android:id="@+id/happen_now" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        </Button>
</LinearLayout>
</LinearLayout>
4

3 回答 3

5

TextView的 inLinearLayout被设置为android:layout_width="fill_parent"and android:layout_height="fill_parent",这意味着它将占据整个空间LinearLayout,没有空间给Button。如果您使用hierarchyviewerSDK 附带的工具,您可以在查看活动时看到这一点。

您需要将 的高度设置TextViewwrap_content或以其他方式为其留出空间Button

于 2010-06-10T23:02:00.290 回答
1

将其可见性设置为 true 后,如下所示:

  • vgAddView.setVisibility(View.VISIBLE);- 展示它
  • vgAddView.setVisibility(View.GONE);- 隐藏它
于 2012-02-11T11:11:34.633 回答
0

你的第一个 TextView 有一个 layout_width="fill_parent"。这意味着,您的 TextView 不会为 Button 留出空间。您可以尝试设置 layout_weight 属性或设置 layout_width 为 wrap_content

于 2015-04-23T17:17:31.310 回答