背景:
我有一个 TextView
和 TextView
右侧的两个(自定义)按钮。 2 个按钮和 TextView
包含在单个 RelativeLayout
中。现在,在任何时候,button-1 或 button-2 都将可见。因为我不知道哪个按钮是可见的,所以在我的代码中为这两个按钮添加了
android:layout_toRightOf="@+id/MyTextView"
XML 文件中的属性。哪个工作正常。这将根据 TextView
的位置(和宽度)放置按钮。
问题:
更改语言后,我必须根据右侧 Button 的宽度更改 TextView 的宽度。我也为按钮和 TextView
尝试了 minWidth/maxWidth
,但它们仍然不允许动态调整 TextView
的大小按钮的宽度。
无论如何我可以指定:
android:layout_toLeftOf="@+id/button1 | @+id/button2"
即 TextView
在 button-1 或 button-2 的左侧。这样 TextView 将根据右侧的按钮对齐?
注意:
我更喜欢需要更改 XML 布局文件(静态)而不是在 java( Activity )文件中添加代码的解决方案。
最佳答案
您可以将这两个按钮放在一个 RelativeLayout
中,并将那个 RelativeLayout
放在另一个 RelativeLayout
中,然后将 TextView
code> 作为 RelativeLayout
的 alignLeft
:
我的应用程序中的示例代码:您需要根据自己的情况进行修改
<RelativeLayout
android:id="@+id/header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<ImageView
android:id="@+id/settings_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="2dip"
android:contentDescription="@string/setting_desc"
android:src="@drawable/top_settings"
android:visibility="gone" />
<ImageView
android:id="@+id/logout_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/settings_img"
android:contentDescription="@string/logout_desc"
android:src="@drawable/top_quit"
android:visibility="gone" />
</RelativeLayout>
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/image_container"
android:layout_centerVertical="true"
android:contentDescription="@string/logo_desc"
android:src="@drawable/top_logo" />
</RelativeLayout>
因此,当任何 ImageView
可见时,您的 View 将得到很好的调整。
关于Android-将 TextView 的宽度设置为按钮 1 或按钮 2 的左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25112992/