我想在android中创建一个自定义视图,我可以像LinearLayout
一样使用它。自定义视图应该在布局中添加的任何内容上添加两个TextView
。我如何控制自定义视图内容的可见性?我必须能够在设计时和运行时添加内容,但应该切换嵌套内容的可见性,但不能使用这两个TextView。 我的问题有点类似于this one这并没有得到满意的答案。Android - 如何控制自定义视图中嵌套视图的可见性?
我已经创建了一个延伸的自定义视图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_content
LinearLayout
。或者我在想这一切都是错误的?嵌套的内容是否在自定义视图的最底部结束,如果是,我如何切换其可见性,而不是我的两个TextView
?
太棒了!这就像我正在寻找的东西。 – kumaheiyama 2014-10-17 22:29:47