2011-09-04 19 views
9

私はAndroidのレイアウトを作成しようとしています.3つのコンポーネントが垂直なLinearLayout内にあります。中央のコンポーネントは、TextViewを含むScrollViewです。 TextViewに大量のテキストが含まれている場合(画面に収まらない場合)、ScrollViewは画面の一番下まで拡大し、スクロールバーを表示し、最後のコンポーネントであるLinearLayoutButtonの内側にプッシュします。画面。

TextView内のテキストがScrollView内に十分に短い場合、画面下部のボタンは完全に配置されます。

私が達成しようとしているレイアウトは次のとおりです。ScrollViewとLinearLayoutでの問題

​​

私が書いたレイアウトのXMLは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_marginBottom="10dip" 
      android:text="Title /> 

    <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

     <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:autoLink="web" 
       android:textColor="#FFFFFF" 
       android:background="#444444" 
       android:padding="10dip" /> 

    </ScrollView> 

    <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 

     <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 

     <Button android:id="@+id/login_button" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:layout_weight="1" 
       android:text="@string/next_button"/> 

    </LinearLayout> 

</LinearLayout> 

答えて

6

scrollviewは、第2のビューオブジェクトであり、 wrap_contentに設定されています。これは画面以上です。

私はRelativeLayoutをお勧めします。最初にandroid:alignParentTop="true"でトップテキストビュー、次にandroid:alignParentBottom="true"で下部のLinearLayout、最後にxmlにスクロールビューが表示され、値はandroid:alignBelow="@id/whatYouCallTheHeaderです。

これは、サイズに関係なく、画面下部の下部バーと上部のヘッダーを揃えます。ヘッダーとフッターが配置された後、スクロールビューは独自の場所を持ちます。

+0

です。 2.1+は2回のパスを行います。 – Phobos

2

LinearLayoutではなくrelativeLayoutに行ってください。そして、alignBelowとallのようないくつかのプロパティを使うことができます。

2

ScrollViewにレイアウトウェイトを追加してみてください。

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 

これは、あなたが提示しているものとほぼ同じような状況で私のために働いたが、それは直感に反しているので、私は、なぜ疑問に残っている(0からあなたの場合、デフォルトのコントロールのレイアウト重量を増加layout_weightを指定しないでください)を1に設定すると、既にあまりにも多くのスペースを使用しているコントロールを小さくする必要があります。

layout_weightを指定しないと、他のコントロールと比較してレイアウトがスクロールビューのサイズを無視できるようになり、逆に1つを指定すると、それに比例して縮小する権限が与えられますあなたが割り当てる体重。

2

![固定ヘッダ・フッタとスクロール可能なボディレイアウト] [1]


これは、あなたが探しているものです。アンドロイドのアプリのほとんどはこのタイプのレイアウトを持っていました。 固定ヘッダーとフッターとスクロール可能な本体です。あなたはそれが一つだけでは、XMLを通過しますので、任意の参照は、それが参照される前にレイアウトする必要があるターゲットとして1.6のために書いている場合、このレイアウトのXMLは


<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
     android:background="#5599DD" 
     android:layout_height="fill_parent"> 
     <!-- Header goes here --> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_marginBottom="10dip" 
      android:textSize="20sp" 
      android:layout_gravity="center" 
      android:text="Title" /> 
     <!-- Body goes here --> 
     <ScrollView 
      android:layout_weight="1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:autoLink="web" 
       android:text="@string/lorem_ipsum" 
       android:textColor="#FFFFFF" 

       android:padding="10dip" /> 
     </ScrollView> 
     <!-- footer goes here --> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
       <Button 
        android:id="@+id/login_button" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_gravity="bottom" 
        android:layout_alignParentRight="true" 
        android:text="Button"/> 

      </RelativeLayout> 
    </LinearLayout> 
</LinearLayout> 
関連する問題