将子项添加到LinearLayout并设置可见性

时间:2013-06-14 20:01:07

标签: android visibility android-view textview onclicklistener

我有一个LinearLayout,其可见性直接受到TextView点击的影响。此LinearLayout内部动态添加了更多TextView。我的LinearLayout viewQuickLinks开始时已经消失了。在我的oncreate中,我调用addQuickLinks,然后将几个TextView添加到LinearLayout。这些TextView都没有设置可见性。我单击TextView将LinearLayout更改为可见,并添加空格,但没有TextView。

我的xml文件(只是为了在滚动视图中添加一个注释):

    <TextView
        android:id="@+id/textQuickLinksTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/navigation_expand"
        android:text="@string/quick_links_title"
        android:textSize="25sp"
        android:visibility="visible" />

    <LinearLayout
        android:id="@+id/viewQuickLinks"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:visibility="gone"
        android:orientation="vertical" />

将LinearLayout更改为可见并消失:

private void setUpQuickLinks() {
    final TextView quickLinksTitleText = (TextView) findViewById(R.id.textQuickLinksTitle);
    quickLinksTitleText.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            LinearLayout viewQuickLinks = (LinearLayout) findViewById(R.id.viewQuickLinks);
            if (viewQuickLinks.getVisibility() == View.VISIBLE){
                viewQuickLinks.setVisibility(View.GONE);
                quickLinksTitleText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.navigation_expand, 0);
            }
            else{
                viewQuickLinks.setVisibility(View.VISIBLE);
                quickLinksTitleText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.navigation_collapse, 0);
            }
        }
    });
    quickLinksClickListeners();
}

为什么当LinearLayout可见时TextViews不会出现?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

尝试将android:layout_height更改为fill_parent。为什么是1浸?