Android 如果为 true,则显示一半 TextView 和一半 ImageView,否则仅显示 TextView
Android display half TextView and half ImageView if true else display only TextView
当 "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 以便在需要时 hide/show 仅对齐 ImageView。
我是 Android 开发的新手,所以请保持温柔 :)
在这些情况下使用相对布局时,您可以对 TextView 使用 alignParentLeft=true,对 ImageView 使用 alignParentRight=true,但如果您希望 TextView 在 ImageView 可见或不可见时进行调整。使用像
这样的东西
<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>
对于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);
当 "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 以便在需要时 hide/show 仅对齐 ImageView。
我是 Android 开发的新手,所以请保持温柔 :)
在这些情况下使用相对布局时,您可以对 TextView 使用 alignParentLeft=true,对 ImageView 使用 alignParentRight=true,但如果您希望 TextView 在 ImageView 可见或不可见时进行调整。使用像
这样的东西<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>
对于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);