2 つのボタンと 1 つのテキストビューを備えたコマンド ヘッダーがあります。テキストビューは画面の中央にあります。ボタン 1 は親の右側にあり、ボタン 2 は親の左側にあります。
ここで、2 つのボタンの間のスペースの中央ではなく、親の中央にテキスト ビューを表示したいと考えています。
textview のテキストの長さは、3 語でも 10 語以上でもかまいません。
長さが10語を超える間、2つのボタンの上にテキストビューを重ねたくありません。また、単語が3つしかないときに、画面の中央にテキストビューが必要です。
以下のコードを使用している場合、単語が 3 ~ 4 個しかない場合に画面の中央にテキストビューが水平に表示されませんが、以下のコードは 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 個以上の単語がある場合は下のコードが重なります。
<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 はオーバーラップしてはなりません。
- 単語が少ない間は、テキストビューを中央に配置する必要があります。
皆さんが私の問題を理解してくれることを願っています。ご不明な点がございましたら、お問い合わせください。