0

私はAndroidが初めてで(Visual Studioで開発するために使用されていました...)、これまでのところレイアウトに疑問があります:

私がやりたいのは、上部にバーがあるレイアウト (ボタンや追加情報を配置するためだけ) と、画面の残りの部分を埋めるスクロール ビューです。これは私がこれまでにやっている方法です:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Home" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/white" >

    <!-- I can put any button or text I want in here -->

</LinearLayout>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp"

    android:background="@drawable/wallpaper" >


</ScrollView>

これは期待どおりに機能していますが、私の質問は、正しい方法で行っているのか、より良い方法 (最も効率的な方法) で行うべきかということです。

前もって感謝します

4

1 に答える 1

1

絶対配置はお勧めしません。たとえば、高さを 50 dp から 100 dp に増やす必要がある場合、いくつかの異なる場所でこの値を変更する必要があります。

レイアウトを改善する方法を少なくとも 2 つ知っています。

1) RelativeLayout (android:layout_below="@id/linearLayout1"または対応する layout_above)の機能を使用する

<RelativeLayout ...>
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/white">
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/linearLayout1"
        android:fillViewport="true"
        android:background="@drawable/wallpaper" >
    </ScrollView>
</RelativeLayout>

2)LinearLayoutに置き換えます(および使用しますandroid:layout_weight="1.0"

<LinearLayout ...>
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/white">
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        android:fillViewport="true"
        android:background="@drawable/wallpaper" >
    </ScrollView>
</LinearLayout>

android:layout_height="0dip"が奇妙に見える場合があります。実際には を使用できますmatch_parentが、Eclipse IDE はそのような行を強調表示し、0dip指定した場合は使用を推奨しますandroid:layout_weight

また、スクロールビューに追加android:fillViewport="true"しました。必要に応じて、スクロールビュー内のコンテンツが最大の高さまで拡大されることを示します。このプロパティについては、こちらを参照してください。

于 2013-03-16T09:27:12.777 に答える