我目前在Android项目中使用此XML布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="bhavik.slidingmenu.activity.LogFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="495dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relLayout1"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.rockerhieu.emojicon.EmojiconTextView
android:id="@+id/txtEmojicon1"
android:text="\ue11b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="45dp"
android:textIsSelectable="true"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="35dp"
android:layout_marginStart="20dp" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar1"
android:max="10"
android:progress="0"
android:layout_alignTop="@+id/txtEmojicon1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="8dp"
android:layout_marginLeft="90dp"
android:layout_marginRight="75dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relLayout2"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.rockerhieu.emojicon.EmojiconTextView
android:id="@+id/txtEmojicon2"
android:text="\ue11b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="45dp"
android:textIsSelectable="true"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="35dp"
android:layout_marginStart="20dp" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar2"
android:max="10"
android:progress="0"
android:layout_alignTop="@+id/txtEmojicon2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="8dp"
android:layout_marginLeft="90dp"
android:layout_marginRight="75dp" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fab_ic_add"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/fab_button_margin_bottom"
android:layout_marginRight="@dimen/fab_button_margin_right"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
app:borderWidth="0dp"/>
</RelativeLayout>
正如您在代码中看到的,我有两个名为relLayout1和relLayout2的相对布局。让我们说根据条件,我想只显示relLayout1而不是relLayout2,我该怎么做?或者反过来?
答案 0 :(得分:0)
如果要隐藏某些内容,可以将visibility属性设置为不可见或消失。
在xml中它将是android:visibility =“invisible”
以编程方式,它将是myView.setVisibility(View.INVISIBLE);
答案 1 :(得分:0)
根据条件,你需要set view visibility,让我们说你需要在某些条件下显示RelativeView1,否则需要显示RelativeView2。
if(condition) {
relView1.setVisibility(View.VISIBLE);
relView2.setVisibility(View.GONE);
}
else {
relView2.setVisibility(View.VISIBLE);
relView1.setVisibility(View.GONE);
}
答案 2 :(得分:-1)
in your Activity
RelativeLayout relLayout1 = (RelativeLayout) findViewById(R.id.relLayout1);
RelativeLayout relLayout2 = (RelativeLayout) findViewById(R.id.relLayout2);
if (condition == true){
relLayout1.setVisibility(View.VISIBLE);
}else{
relLayout2.setVisibility(View.VISIBLE);
}
and in your xml
<RelativeLayout
android:id="@+id/relLayout1"
android:visibility="gone"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/relLayout2"
android:visibility="gone"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">