1

いくつかのTextViewを保持するためにビューが必要ですが、残念ながら正確な数はわかりません。TextViewsを並べ替えるには、スタックを並べて表示する必要があります(Tetrisを想像してみてください。ただし、長方形または正方形のみを使用します)。私がそれについて考えようとした方法は、LinearLayoutを使用してそれらを水平または垂直に保持することです。次に、それらが互いに隣接している場合は、それらのウェイトを使用して適切にストレッチします。それ以外の場合は、垂直方向を使用します。問題は、より複雑なスタックを持つネストされたLinearLayoutのパフォーマンスです。私はRelativeLayoutを使用することを考えましたが、TextViewがオーバーラップしないようにする必要があるため、それは機能しません。したがって、それらが互いに隣接している場合のように、それぞれが十分なスペースを均等に取る必要があります。layout_weightを使用すると、うまく機能します。

これが私に警告を与えている例です(私がプログラムで行っていることの簡単な例だけです):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello World 1"
        android:background="#000000"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello World 3"
        android:background="#000000"
        android:layout_weight="1"/>
        <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello World 4"
        android:background="#000000"
        android:layout_weight="1"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World 2"
        android:background="#000000" />
</LinearLayout>

ちなみにRelativeLayoutで試してみましたが、TextViewの1つは設定されたサイズである必要がありますが、スペースを均等に取る必要があります。具体的なサイズはわかりません。画面の幅を測定してそのように分割することも、不格好で正確ではありません。皆様からのご意見をお待ちしております。

編集:それで私はそれについてもう少し考えていて、RelativeLayoutを使用する解決策を考えました。Androidドキュメントには良い例がありますが、唯一の問題は、ビューの1つを設定サイズにして、もう1つを拡張できるようにする必要があることです。しかし、それらすべてに設定されたサイズを持たせないようにする方法があれば、それも機能する可能性があるので、それらは伸びることができます。誰かがいつかそれをやろうとしましたか?

4

2 に答える 2

3

最終的なユースケースがどのようになるかは少し不明確なので、これで問題が完全に解決するかどうかはわかりませんが、TableLayoutドキュメントリンク)をご覧ください。これはに基づいてLinearLayoutいるため、アイテムは特定のスペースを均等に占めるように定義できますが、行と列の観点から位置とスパンを定義できます。

したがって、たとえば、サンプルコードは次のようになります。

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="*" >
    <!-- Wrap all items in a given row in a TableRow -->
    <TableRow>
        <TextView
            android:text="Hello World 1"
            android:background="#000000"/>
        <TextView
            android:text="Hello World 3"
            android:background="#000000"/>
        <TextView
            android:text="Hello World 4"
            android:background="#000000"/>
    </TableRow>
    <!-- If a child occupies an entire row, it can be by itself -->
    <TextView
        android:text="Hello World 2"
        android:background="#000000"/>
</TableLayout>

android:layout_columnまた、属性とandroid:layout_span属性を使用して、アイテムを含める必要のある列と、アイテムが占める列の数を正確に定義することもできます。また、TableLayout適用TableRowされたレイアウトパラメータを基本的に無視し、非常に具体的な(文書化された)パラメータを使用しているため、コードにそれらを追加すると、実際に何が起こっているのか混乱するだけであることに注意してください。もちろん、これはすべてプログラム的にもXMLでも構築できます。

これで必要な柔軟性が得られない場合は、拡張またはに基づく独自のカスタムレイアウトを作成することをお勧めしますLinearLayout。このメカニズムは、体重のある子供を測定するために使用されますが、それほど複雑ではありません。測定後に各子供がどのように配置されるかをオーバーライドできます。プラットフォームがこれらのことをどのように行うかを確認したい場合は、次のソースリンクを参照してください。LinearLayout

于 2012-11-26T20:45:15.897 に答える
3

更新:パーセントサポートライブラリはAPIレベル26から非推奨になっていることがわかっています。これはConstraintLayout、同じフラットxml構造を実現するための新しい方法です。

更新されたGithubプロジェクト

更新されたサンプル:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/fifty_thirty"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ffff8800"
        android:gravity="center"
        android:text="@string/fifty_fifty_text"
        android:textColor="@android:color/white"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.5"
        android:textSize="25sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.5" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ffff5566"
        android:gravity="center"
        android:text="@string/fifty_fifty_text"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.5"
        app:layout_constraintLeft_toRightOf="@id/fifty_thirty"
        app:layout_constraintTop_toBottomOf="@id/fifty_thirty"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.5" />

</android.support.constraint.ConstraintLayout>

非推奨

更新:同じためにAndroidサポートライブラリを使用できます。

compile 'com.android.support:percent:23.0.0'

画面を50〜50%に分割するために、このデモを検討してください。

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/fifty_huntv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff7acfff"
        android:text="20% - 50%"
        android:textColor="@android:color/white"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@id/fifty_huntv"
        android:background="#ffff5566"
        android:text="80%-50%"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="50%"
        />

</android.support.percent.PercentRelativeLayout>

出力:

パーセントサポートライブラリデモ

ここにデモ!!!

GitHubプロジェクトはこちら!!!

于 2015-09-06T07:48:10.627 に答える