【问题标题】:Android - how to control visibility of nested views within a custom view?Android - 如何控制自定义视图中嵌套视图的可见性?
【发布时间】:2014-10-17 19:17:43
【问题描述】:

我正在尝试在 android 中创建一个自定义视图,我可以像 LinearLayout 一样使用它。自定义视图应该在我在布局中添加的任何内容之上添加两个 TextView。如何控制自定义视图内容的可见性?我必须能够在设计时添加内容,并且在运行时应该切换嵌套内容的可见性,但不能切换这两个 TextView。 我的问题有点类似于 this one 并没有真正得到满意的答案。

我创建了一个扩展 LinearLayout 的自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    >
<TextView
    android:id="@+id/txt_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="My title"
    android:layout_marginLeft="10dp"
    />
<TextView
    android:id="@+id/txt_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:layout_marginRight="10dp"
    android:text="My button"
    android:onClick="txtButton_onClick"
    android:clickable="true"
    />
</LinearLayout>
<LinearLayout
    android:id="@+id/all_contents"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_marginTop="10dp"
    />
    <!-- This is where I _want_ to add all nested contents to make it easy to toggle its visibility -->
</merge>

我希望将所有嵌套内容包含在底部的 LinearLayout 中,这样当我单击 txt_button 时,我可以将 all_contents 的可见性设置为 gone 并使其消失。我仍然希望 txt_titletxt_button 继续可见,所以我可以切换是否显示内容。

自定义视图应该这样调用:

<org.my.app.view.SlidingLayoutView
    android:id="@+id/slv_date_time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    custom:titleString="Date and time"
    custom:buttonString="Change">
    <!-- This is where all nested contents should go -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Example nested content"
        />
</org.my.app.view.SlidingLayoutView>

我不明白如何确定嵌套内容进入all_contentLinearLayout。还是我在想这一切都错了?嵌套内容是否最终位于自定义视图的最底部,如果是,我如何切换其可见性而不是我的两个 TextView

【问题讨论】:

    标签: android android-layout android-linearlayout android-custom-view


    【解决方案1】:

    上面的意思是线性布局的上面吗?我认为如果你取出 all_contents LinearLayout,你的 xml 应该可以工作,东西将被添加到 LinearLayout。让你的 xml 像这样。

        <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        >
        <TextView
            android:id="@+id/txt_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="My title"
            android:layout_marginLeft="10dp"
            />
        <TextView
            android:id="@+id/txt_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_marginRight="10dp"
            android:text="My button"
            android:onClick="txtButton_onClick"
            android:clickable="true"
            />
    </LinearLayout>
    <!-- This is where I _want_ to add all nested contents to make it easy to toggle its visibility -->
    </merge>
    

    我会像这样在类中添加两个方法,您可以使用它们来切换内容的可见性或顶部的 textView:

    public void setTopTextViewVisibility(int visibility){
        for(int i = 0; i < getChildCount(); i ++){
            View view = getChildAt(i);
            if(view.getId() == R.id.txt_title || view.getId() == R.id.txt_button){
                view.setVisibility(visibility);
            }
        }
    }
    
    public void setContentVisibility(int visibility){
        for(int i = 0; i < getChildCount(); i ++){
            View view = getChildAt(i);
            if(!(view.getId() == R.id.txt_title || view.getId() == R.id.txt_button)){
                view.setVisibility(visibility);
            }
        }
    }
    

    【讨论】:

    • 太棒了!这就是我正在寻找的东西。
    【解决方案2】:

    我会使用 FrameLayout,您可以在框架内放置一个线性布局,然后将所有视图放入其中,并为每组视图创建一个新框架。 这是我所做的菜单屏幕的基本结构,一旦您单击一个按钮,它就会显示一个带有更多选项的新框架。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mayne" >
    
    <FrameLayout
        android:id="@+id/frameMain" >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
    <TextView
        android:id="@+id/textView1" />
    
    <Button
        android:id="@+id/button1" />
    
    <Button
        android:id="@+id/button2" />
    
        </RelativeLayout>
    
    </FrameLayout>
    
    <FrameLayout
        android:id="@+id/frameDiff" >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <Button
                android:id="@+id/btnMed" />
    
            <Button
                android:id="@+id/btnHard" />
    
            <Button
                android:id="@+id/btnEasy" />
    
            <TextView
                android:id="@+id/textView2" />
    
            <Button
                android:id="@+id/btnCancel" />
    
        </RelativeLayout>
    
    </FrameLayout>
    

    【讨论】:

    • 我不太确定我明白你的意思。你能给我一个 XML 的例子吗?
    猜你喜欢
    • 2023-04-02
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多