【问题标题】:Android display half TextView and half ImageView if true else display only TextView如果为真,Android 显示一半 TextView 和一半 ImageView 否则仅显示 TextView
【发布时间】:2017-08-02 10:42:01
【问题描述】:

当“true”时,我试图在屏幕左侧的 TextView 内显示文本,在右侧的 ImageView 内显示图像:

<RelativeLayout
    android:id="@+id/displayMessageCenter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/displayMessageTop"
    android:layout_centerHorizontal="true"
    android:background="@android:color/white">
    <TextView
        android:id="@+id/textMessageText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="#000000"
        android:layout_weight="0.5"
        android:layout_alignBottom="@+id/dividerId" />
    <ImageView
        android:id="@+id/messagePicture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignBottom="@+id/dividerId"
        android:layout_weight="0.5"
        android:gravity="right"/>
    <View
        android:id="@+id/dividerId"
        android:layout_width="400dp"
        android:layout_height="1dp"
        android:background="#000"
        android:layout_above="@id/leftTextGenericDialog"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>
    <TextView
        android:id="@+id/leftTextGenericDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textColor="#FF72CCCC"
        android:text="SEND RESPONSE"
        android:textSize="40dp"
        android:drawableLeft="@drawable/sendicon"
        android:layout_above="@+id/closeButton"
        android:onClick="sendMessage"/>

    <ImageButton
        android:id="@+id/closeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5sp"
        android:layout_gravity="center"
        android:background="@drawable/button_close_animation"
        android:onClick="closeActivity"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

如果为“false”,则隐藏图像并仅显示 TextView,文本居中。隐藏将是这样的:

if(true) imageView.setVisibility(View.GONE);
else imageView.setVisibility(View.VISIBLE);

但我不知道如何对齐 ImageView 和 TextView 以便在需要时更容易隐藏/显示 ImageView。

我是 Android 开发新手,所以请温柔:)

【问题讨论】:

  • android:layout_weigh 仅适用于 LinearLayout ... 将不可见视图放置在大小为 1x1 的父级中心,然后将 textMessageText 放在此左侧(并对齐父级左侧)和 messagePicture 在此右侧((并对齐父级左侧))都与 0dp

标签: android android-layout show-hide


【解决方案1】:

在这些情况下使用相对布局时,您可以对 TextView 使用 alignParentLeft=true,对 ImageView 使用 alignParentRight=true,但如果您希望在 ImageView 可见或不可见时调整 TextView。使用类似的东西

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

  <TextView
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:text="text"
      android:layout_weight="0.5"
      android:gravity="center_horizontal"
      />

  <ImageView
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="0.5"
      android:visibility="gone"

      />

</LinearLayout>

【讨论】:

    【解决方案2】:

    对于true条件,你可以设置

    android:alignParentLeft="true"
    

    android:alignParentRight="true"
    

    对应到你的 TextView 和 ImageView。


    对于 false 条件,您可以以编程方式为您的 TextView 设置规则。只需使用 findViewById 获取 TextView 并设置如下规则:

    TextView tv = (TextView) findViewById(R.id.textMessageText);
    RelativeLayout.LayoutParams layoutParams =
                    (RelativeLayout.LayoutParams)tv.getLayoutParams();
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    tv.setLayoutParams(layoutParams);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多