2011-11-25 17 views
19

私は以下のXMLを持っています。私は私のScrollViewの下に静的なボタンを作ろうとしています。私は体重を設定しようとしました、ボタンをスクロールビューの下に設定しました。誰かがボタンを下に置くことができる方法を私に与えることができ、スクロールビューは画面の中央だけを占有しますか?ScrollViewの下に静的ボタンを作成する方法は?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 



    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/menuButtons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 

     <Button xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/newItems" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Items" /> 
     <Button xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/categories" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Categories" /> 

    </LinearLayout> 


    <ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/contentScroller" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/menuButtons"> 

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


      <TableLayout 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:stretchColumns="1" > 


       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip" > 
        <ImageView 
         android:src="@drawable/thumbdrive"/>" 
        <TextView 
         android:layout_column="1"    
         android:text="Thumb Drives"    
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <ImageView 
         android:src="@drawable/laptop"/> 
        <TextView 
         android:layout_column="1" 
         android:text="Laptops" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <ImageView 
         android:src="@drawable/sdcard"/> 
        <TextView 
         android:layout_column="1" 
         android:text="SD Cards" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
      </TableLayout> 
     </LinearLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/submit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Submit New Item" 
     android:layout_below="@id/contentScroller"/> 

</RelativeLayout> 
+0

私は昨日全く同じ問題を解決しましたが、私は別のトリックを見つけました。好奇心で、視覚モードでのビジュアルはどうですか?ありがとうございます – tuandroid

+0

ランドスケープでうまく見えます。 – kieblera5

答えて

29

RelativeLayoutを使用してください。一番下のButtonから始めて、の上にScrollViewを置きます。

Relative Layout - Android Developers

<RelativeLayout 
    (...)> 

    <LinearLayout android:id="@+id/ll1" 
     android:layout_alignParentTop="true" 
     (...)/> 

    <Button android:id="@+id/button" 
     android:layout_alignParentBottom="true" 
     (...)/> 

    <ScrollView 
     android:layout_above="@id/button" 
     android:layout_below="@id/ll1" 
     (...)/> 

</RelativeLayout> 

このような何か。私の頭の中に書かれているので、いくつかのエラーが発生する可能性があります。

+0

ありがとうございます。私は以前これを試していましたが、スクロールビューの後にボタンがありましたので、うまくいきませんでしたが、それはあなたが書いた方法で動作します。 – kieblera5

+1

これは 'RelativeLayout'をそのコンテナの底まで伸ばします。これは大丈夫かもしれません。 _ RelativeLayoutのサイズとその子の位置との間に循環依存関係を設定することはできません。たとえば、高さがWRAP_CONTENTに設定され、子がALIGN_PARENT_BOTTOM._に設定されたRelativeLayoutを持つことはできません。 - [RelativeLayoutリファレンス](http://developer.android.com/reference/android/widget/RelativeLayout.html) – jtooker

+0

perfect 1つは私の問題です...(y) – Elizabeth

0

場所relativeLayoutでボタンと親一番下にそれを揃える:

<RelativeLayout android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:orientation="horizontal" 
    android:layout_height="50px"> 
<Button 
    android:id="@+id/submit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="Submit New Item" 
    android:layout_below="@id/contentScroller"/> 
</RelativeLayout> 
48

私はこれが遅れて本当に知っているが、よりよい解決策があります。 RelativeLayoutアプローチの問題点とボタンを相対レイアウトの一番下に合わせることは、レイアウトの高さをfill_parent(またはmatch_parent)と本質的に同じにすることです。

次のようにこれを行うための適切な方法は次のとおりです。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:fillViewport="true"> 
      <!-- Your Scrollview content goes here --> 
    </ScrollView> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center_horizontal" 
     android:text="Button Text Goes Here" /> 
</LinearLayout> 

キーが0に高さを設定し、それを1のレイアウト重みを与えることです...私は読んでから伝えることができるものから、レイアウトサイジングプロセスでは、サイズを0に設定し、レイアウトのウェイトを設定すると、レイアウト内のすべての子プロセスを処理するまでビューのサイジングが保留されます。次に、2回目のパスで回って、スクロールビューのサイズを適切に調整します。

+3

ありがとう!完璧に私のために働いた(私はまだ相対レイアウトの周りに私の頭を持っていない)。 – IanB

+0

ありがとうございます。 あなたのソリューションでは、 'ScrollView'の内容が少ない(短い)場合でも、そのコンテナの底まで伸びない**(外)LinearLayout **を持つことができましたが、 (RelativeLayoutを使用し、 'Button'が' ScrollView'の下にあるように) '1px'にスケックすることなく、' Button'を下に置きます。 「LinearLayout」ストレッチを気にしない(スクロール領域を常に伸ばしてできるだけ多くのスペースを埋める)気にしない場合は、他の解決策のいくつかが私には明らかです。 – jtooker

+3

ベストソリューション私はこれまで見てきました。 –

関連する問題