-1

私は5 buttonsを配置したいアクティビティを設計しています。そのためには、下の図に示すように、すべてのボタンを含む体系的なレイアウトを設計したいと考えています。 Activityアンドロイドでレイアウトコンポーネントを反応的にするには?

このようなレイアウトをハードコードされた値でデザインすることに成功しています。しかし、異なるサイズの画面でテストしながら、破壊されたUIが発生します。私の質問は、複数の画面解像度のアプリUIをデザインしたい場合、どうすればいいのですか? wrap_contentmatch_parentを使用してデザインする方法はありますか?検索では、画面サイズに適した単一のアクティビティに対して異なるレイアウトを設計できるという1つのソリューションが得られました。しかしそれは信頼できますか? 私のXMLコードを以下に示します。

<?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:background="@drawable/gradient" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="30dp" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/upbtn" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:layout_width="match_parent" 
     android:layout_height="90dp" /> 

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


     <Button 
      android:id="@+id/leftbtn" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:layout_width="100dp" 
      android:layout_height="150dp" /> 


     <Button 
      android:id="@+id/centerbtn" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:layout_width="100dp" 
      android:layout_height="150dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginStart="10dp"/> 


     <Button 
      android:id="@+id/rightbtn" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:layout_width="100dp" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginStart="10dp"/> 

    </LinearLayout> 

    <Button 
     android:id="@+id/downbtn" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:layout_width="match_parent" 
     android:layout_height="90dp" /> 
    </LinearLayout> 
</LinearLayout> 
+0

リニアレイアウトをウェイトとともに使用することも、新しいコンストレインレイアウトを使用することもできます –

答えて

0

このレイアウトを使用して、私はあなたのニーズや要件に一致するすべてのビューに関連する高さと幅ごとに重みを追加しています。すべてのデバイスでテストされ、よく見えます。最初のレイアウトのマージントップを削除しましたが、好きな場合は残しておくことができます。

<?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"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="4" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/upbtn" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="0dp" /> 

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

     <Button 
      android:id="@+id/leftbtn" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" /> 


     <Button 
      android:id="@+id/centerbtn" 
      android:layout_weight="1" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      /> 


     <Button 
      android:id="@+id/rightbtn" 
      android:layout_weight="1" 
      style="@style/Widget.AppCompat.Button.Colored" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      /> 

    </LinearLayout> 

    <Button 
     android:layout_weight="1" 
     android:id="@+id/downbtn" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" /> 
</LinearLayout> 
</LinearLayout> 
1

ConstraintLayoutを使用すると、作業に適しています。 constriantlayoutでGuideLineを使用すると、ハードコードされた値をスキップし、画面領域の割合に基づいてスペースを分割することができます。ここで

は、始めるためのリファレンスです:https://developer.android.com/training/constraint-layout/index.html

関連する問題