我有带有两个按钮和单个文本视图的命令标题。textview 位于屏幕中央。按钮 1 位于父项的右侧,按钮 2 位于父项的左侧。
现在,我想在父级的中心显示文本视图,而不是在两个按钮之间的空间的中心。
textview 中的文本长度可以是 3 个单词或超过 10 个单词。
当长度超过 10 个字时,我不想在两个按钮上方重叠 textview。而且我想要在屏幕中央的文本视图,而只有 3 个单词。
当我使用下面的代码时,当只有 3 到 4 个单词时,它不会在屏幕中央水平显示 textview,但下面的代码在超过 10 个单词时也不会重叠。
<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"
tools:context=".Webviewdemo" >
<ImageView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/backbtn" />
<ImageView
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/infobtn" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/button2"
android:layout_toRightOf="@+id/button1"
android:gravity="center"
android:singleLine="true"
android:text="123"
android:textSize="22sp"/>
</RelativeLayout>
以同样的方式,当我使用下面的代码时,当只有 3 到 4 个单词但下面的代码重叠而有 10 个以上的单词时,它会在屏幕中央水平显示 textview。
<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"
tools:context=".Webviewdemo" >
<ImageView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/backbtn" />
<ImageView
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/infobtn" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:text="123"
android:textSize="22sp" />
</RelativeLayout>
所以,我的问题是我怎样才能用单个代码实现以上两个目标。
- 当有更多单词时,Textview 不应重叠。
- Textview 应该在中心,而没有单词。
我希望你们都明白我的问题。如果有任何疑问请询问。