2016-06-20 4 views
-6

私は、Androidに次の図のように4つのボタンを設計する必要があります。どのレイアウトを使用すべきか、60-40%の比率をすべての画面でサイズ変更できるようにする方法についてはわかりません。ありがとうございました!Androidで下のフォーマットをデザインするには

enter image description here

+0

ConstraintLayout –

+1

か[PercentLayout](ht tps://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html) – marshallino16

+0

あなたがこれまでに試したことや実際にぶつかったことを追加してください。 –

答えて

1

、およびlayout_height

LinearLayout (Vertical) 
    LinearLayout (Horizontal) //layout_weight: 0.4 
     FrameLayout //layout_weight: 0.4 
     FrameLayout //layout_weight: 0.6 
    LinearLayout (Horizontal) //layout_weight: 0.6 
     FrameLayout //layout_weight: 0.6 
     FrameLayout //layout_weight: 0.4 

注:match_parent0dpに変更した場合は、中に重みを持たなければなりません60%で0.6、40%で0.4のような適切な場所。

enter image description here

コード:

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

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

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.4" 
      android:background="#d00000"></FrameLayout> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.6" 
      android:background="#9be412" /> 
    </LinearLayout> 

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

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.6" 
      android:background="#12b9a3" /> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.4" 
      android:background="#b208a7" /> 
    </LinearLayout> 

</LinearLayout> 
2

あなたは体重属性を使用して、レイアウトを相対することができます。私はあなたのケースに合った例を提供しました。この例では重量和は常に1

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.6"> 

    <Button 
     android:id="@+id/button_1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.6" 
     android:background="@color/red"/> 

    <Button 
     android:id="@+id/button_2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.4" 
     android:background="@color/black"/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.4"> 

    <Button 
     android:id="@+id/button_3" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.4" 
     android:background="@color/green"/> 

    <Button 
     android:id="@+id/button_4" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.6" 
     android:background="@color/orange"/> 
</LinearLayout> 

である。これは、結果3線形レイアウトを使用し

enter image description here

+0

*ウェイトの合計は常に1です。* that that真実ではない。また、ネストされた重みはパフォーマンスの点で悪いことに注意してください。 –

+0

この例では意味しています。私はそれを調整します。 – MrJM

+0

@Zekeこれは当てはまりません。高さが0dpの場合は、0.4が40%を占め、0.6が60%を占める – MrJM

1

であることに留意されたいです。最初の線形レイアウトは垂直、weigthSum = 2は2線形レイアウトの子で、その重みは1で、各子線形レイアウトは水平になり、2つのボタンがあります。

0

プロジェクトに%support libraryを使用してください。

dependencies { 
    compile 'com.android.support:percent:xx.x.x' 
} 
layout_widthへの変更なしに、ドラッグアンドドロップした後
<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"> 

    <View 
     android:id="@+id/top_left" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_alignParentTop="true" 
     android:background="#ff44aacc" 
     app:layout_heightPercent="20%" 
     app:layout_widthPercent="70%" /> 

    <View 
     android:id="@+id/top_right" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/top_left" 
     android:background="#ffe40000" 
     app:layout_heightPercent="20%" 
     app:layout_widthPercent="30%" /> 

    <View 
     android:id="@+id/bottom" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_below="@+id/top_left" 
     android:background="#ff00ff22" 
     app:layout_heightPercent="80%" /> 
</android.support.percent.PercentRelativeLayout> 
0

これがお手伝いします..あなたはまた、重量でのLinearLayout使用することができます

<?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:background="@android:color/black" 
    android:weightSum="100"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="60" 
     android:weightSum="100" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="Button1" 
      android:layout_weight="60"/> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="Button2" 
      android:layout_weight="40"/> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="40" 
     android:weightSum="100" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="Button3" 
      android:layout_weight="40"/> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="Button4" 
      android:layout_weight="60"/> 

    </LinearLayout> 

</LinearLayout> 

おかげで...

関連する問題