1

xmlを使用してAndroidで作成しようとしているレイアウトの例があります。同様のレイアウトを作成することはできますが、アプローチが間違っているように感じます。

これらの場合に私が行っているのは、「行」として機能するように相対レイアウトをネストすることです。下の画像は私が何をするかを示しています。

レイアウト

これに似たレイアウトをどのように作成しますか?相対的なレイアウトをネストするのはやり過ぎのように感じますが、そうでない場合、どうすればすべてを中央に配置できるかわかりません。

使用したレイアウトをネストしなかったとき

android:layout_toRightOf="..."
android:layout_below="@+id/t1"

t5、t6、t7(画像から)。結果は正しくありませんでした。t1、t2、t3、およびt4は、水平方向の中央に配置されなくなりました。

この時点以降のすべてが新しい行に表示されるように相対レイアウトを指示する方法はありますか?それとも、相対的なレイアウトはこのようなものに行くのに間違った方法ですか?各行が必ずしも同じ量のビューを持つ必要はなく、中央に配置する必要があるため、テーブルレイアウトが正しく機能するとは思いません。

どんな提案もありがたいです!

4

4 に答える 4

3

LinearLayouts垂直方向に2つの水平方向をネストできますLinearLayout。おそらくそれほど効率的ではありませんが、RelativeLayout必要なセンタリング動作を簡単に取得できます。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="D" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="E" />
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="F" />
    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="G" />
</LinearLayout>
</LinearLayout>

を使用した同じレイアウトの別のバージョンを次に示しますRelativeLayoutLinearLayouts上記のネストから始めて、[リファクタリング]> [Android]> [レイアウトの変更... ]を選択RelativeLayoutし、を選択して、中央に配置されるまで結果のレイアウトを微調整しました。

私はそれを正しくするためにいくつかのトリックを使用しました。真ん中のボタンを親の中央に配置することから始め、次に左右に構築しました。中央に偶数のボタンとスペースがある一番上の行では、中央の0幅を使用しTextViewてボタンを固定しました。それは少しハッキーだと思いますが、それで仕事は終わります。:-)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
    android:id="@+id/TextView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/hello" />
<TextView
    android:id="@+id/Dummy"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_centerInParent="true"
    android:layout_below="@+id/TextView1" />
<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/Dummy"
    android:layout_toLeftOf="@+id/Dummy"
    android:text="B" />
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button2"
    android:layout_alignTop="@+id/Dummy"
    android:layout_toLeftOf="@+id/button2"
    android:text="A" />
<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button2"
    android:layout_alignTop="@+id/Dummy"
    android:layout_toRightOf="@+id/Dummy"
    android:text="C" />
<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button2"
    android:layout_alignTop="@+id/Dummy"
    android:layout_toRightOf="@+id/button3"
    android:text="D" />
<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_below="@+id/Dummy"
    android:text="F" />
<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/button6"
    android:layout_alignBaseline="@+id/button6"
    android:layout_toLeftOf="@+id/button6"
    android:text="E" />
<Button
    android:id="@+id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button6"
    android:layout_alignTop="@+id/button6"
    android:layout_toRightOf="@+id/button6"
    android:text="G" />
</RelativeLayout>
于 2012-07-19T09:29:32.280 に答える
2

ウェイトを使用して試すこともできます。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2.5"
        android:gravity="center"
        android:text="hello" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="A" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="B" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="C" />

        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="D" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="E" />

        <Button
            android:id="@+id/button6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="F" />

        <Button
            android:id="@+id/button7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="G" />
    </LinearLayout>

</LinearLayout>

ここに画像の説明を入力してください

于 2014-11-05T16:21:03.543 に答える
1

これをチェックしてください:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout 
        android:id="@+id/l1"
        android:layout_width="fill_parent"
        android:layout_height="50dp"></LinearLayout>
    <View 
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="@android:color/white"
        android:layout_below="@+id/l1"
        android:id="@+id/v1"/>

    <LinearLayout 
        android:id="@+id/l2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/v1"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">
    <Button 
        android:id="@+id/pos"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        />
     <Button 
        android:id="@+id/neu"
         android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>
      <Button 
        android:id="@+id/neg"
         android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>
      <Button 
        android:id="@+id/neg"
         android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>
    </LinearLayout>

    <View 
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="@android:color/white"
        android:layout_below="@+id/l2"
        android:id="@+id/v2"
        />
    <LinearLayout 
        android:id="@+id/l3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/v2"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">
    <Button 
        android:id="@+id/pos"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        />
     <Button 
        android:id="@+id/neu"
         android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>
      <Button 
        android:id="@+id/neg"
         android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>

    </LinearLayout>
    <View 
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="@android:color/white"
        android:layout_below="@+id/l3"
        android:id="@+id/v3"
        />
</RelativeLayout>
于 2012-07-19T09:41:57.997 に答える
0

代わりに線形レイアウトを使用することもできます。最初の線形レイアウトでは、重みの合計を4に設定し、レイアウトの重みを1に設定して均等に分割し、各ビューの重力を中心に設定します。線形レイアウトの送信でも同じことを行います。

于 2014-11-04T15:44:35.837 に答える