0

我想在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_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

回答

0

通过对你的线性布局上面的意思是顶?我认为你的xml应该可以工作,如果你取出all_contents LinearLayout,事情将被添加到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> 

我想补充两个方法的类像这样的,你可以用它来切换你的内容的可见性或顶部的textViews:

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); 
     } 
    } 
} 
+0

太棒了!这就像我正在寻找的东西。 – kumaheiyama 2014-10-17 22:29:47

0

我会使用一个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> 

+0

我不太确定我了解你意思是。你能给我一个XML例子吗? – kumaheiyama 2014-10-17 20:17:56

+0

我加了一个例子 – MDubzem 2014-10-17 20:53:01

相关问题