【问题标题】:How to choose what to display from XML Android Development [duplicate]如何从 XML Android 开发中选择要显示的内容 [重复]
【发布时间】:2016-08-03 21:17:01
【问题描述】:

我的 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>

正如您在代码中看到的,我有 2 个相对布局,分别称为 relLayout1 和 relLayout2。假设根据条件,我只想显示 relLayout1 而不是 relLayout2,我该怎么做?还是反过来?

【问题讨论】:

    标签: android xml android-layout android-fragments


    【解决方案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"> 
    

    【讨论】:

    • 完美运行。非常感谢!
    • 如果有帮助请采纳答案:D
    【解决方案2】:

    您需要根据条件set view visibility,假设您需要在特定条件下显示RelativeView1,否则需要显示RelativeView2。

    if(condition) {
      relView1.setVisibility(View.VISIBLE);
      relView2.setVisibility(View.GONE);
    }
    else {
      relView2.setVisibility(View.VISIBLE);
      relView1.setVisibility(View.GONE);
    }
    

    【讨论】:

    • 完美运行。非常感谢!
    【解决方案3】:

    如果您想隐藏某些内容,您可以将可见性属性设置为不可见或消失。

    在 xml 中应该是 android:visibility="invisible"

    以编程方式它将是 myView.setVisibility(View.INVISIBLE);

    【讨论】:

    • 谢谢!让它工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多