0

ボタンの列があり、ボタンの中央のすぐ下にテキストが表示されるアプリを作成しようとしています。これまでのところ、これは私が持っているものです:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/blue"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/red" android:layout_gravity="center"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/green" android:layout_gravity="right"/>

</FrameLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="2.5dip"                       
        android:layout_marginLeft="7.5dip"
        android:text="@string/button1"
        android:gravity="center" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2.5dip"
        android:text="@string/button2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" 
        android:layout_marginTop="2.5dip"           
        android:layout_marginRight="5dip"
        android:text="@string/button3" />

</RelativeLayout>

ただし、テキストはボタンの中央の下に並んでいません。誰か助けてもらえますか?

4

3 に答える 3

0

Eclipseを使用している場合は、レイアウト.xmlのグラフィカルビューに移動し、中央に配置するビューを右クリックして、[レイアウトの垂直または水平]に移動し、[重力]->[中央]をクリックします。

于 2012-04-28T16:51:35.590 に答える
0

まず、FrameLayout内で複数の子を使用するのは決して良いことではありません。可能であれば、LinearLayoutを使用する必要があります。ボタンを画面の幅の3分の1のように塗りつぶしたい場合は、次を使用できます。

<LinearLayout 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:weightsum="3" >

     <Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/blue"
       android:weight="1"/>

     <Button
       android:id="@+id/button2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/red"
       android:weight="1"/>

     <Button
       android:id="@+id/button3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/green"
       android:weight="1"/>
</LinearLayout>

次に、TextViewの場合、LinearLayoutを再度使用できますが、方向を変更します。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="2.5dip"                       
    android:layout_marginLeft="7.5dip"
    android:text="@string/button1"
    android:layout_gravity="center" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="2.5dip"
    android:text="@string/button2"
    android:layout_gravity="center" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" 
    android:layout_marginTop="2.5dip"           
    android:layout_marginRight="5dip"
    android:text="@string/button3"
    android:layout_gravity="center" />

</LinearLayout>

私が間違っていなければ、これはおそらくあなたの問題を解決するでしょう。

于 2012-04-28T16:12:01.447 に答える
0

ドリュー、重力とレイアウト重力の2つのプロパティでそれらを中央揃えできるはずです。重心プロパティを1つだけ使用しても、毎回機能するわけではありませんが、この2つを使用することで、私と同じように魅力的に機能するはずです。

android:gravity="center" android:layout_gravity="center"
于 2012-04-28T18:12:46.617 に答える