0

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>

したがって、私の質問は、単一のコードで両方を達成するにはどうすればよいかということです。

  1. より多くの単語がある間、Textview はオーバーラップしてはなりません。
  2. 単語が少ない間は、テキストビューを中央に配置する必要があります。

皆さんが私の問題を理解してくれることを願っています。ご不明な点がございましたら、お問い合わせください。

4

4 に答える 4

0
 <LinearLayout
    android:id="@+id/panelIconLeft1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="5dp" >

    <Button
        android:id="@+id/btnHome1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="LOCATION"
        android:onClick="btnHomeClick" />
</LinearLayout>

<TextView
    android:id="@+id/txtHeading1"
  style="@style/heading_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/panelIconRight1"
    android:layout_toRightOf="@id/panelIconLeft1"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"

    android:singleLine="true"
    android:text=""
    android:textColor="@android:color/white" />

<LinearLayout
    android:id="@+id/panelIconRight1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_margin="5dp" >

    <Button
        android:id="@+id/btnFeedback1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Post Ad"
        android:onClick="btnFeedbackClick" />
</LinearLayout

上記のコードはあなたを助けるでしょう

于 2013-09-11T12:31:18.163 に答える
0

このコードを試してください

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_toLeftOf="@+id/button2"
    android:layout_toRightOf="@+id/button1"
    android:gravity="center"
    android:singleLine="true"
    android:text="123"
    android:textSize="22sp"/>

編集 この行を削除

  android:layout_centerInParent="true"
于 2013-09-11T11:25:25.033 に答える