【发布时间】: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_title 和 txt_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