71

私は相対レイアウトを使用しており、画面の下部にボタンを設定したいのですが、これによりすべてが下部に配置され、マージンが必要になるため、画面の端との間にスペースがあります/ビューとボタン。しかし、私がボタンマージンを何をしても、何らかの理由で2.1以降では何もしません。相対的なレイアウトには背景が含まれているので、その余白はできません。

誰でもこれの修正を知っていますか?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:background="@drawable/background" >

    <Button
        android:id="@+id/confirm_mobile_button_next"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="false"
        android:layout_margin="15dp"
        android:background="@drawable/button_shape_selector"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:text="@string/confirm_mobile_no_continue"
        android:textColor="@color/white"
        android:textStyle="bold" />

</RelativeLayout>
4

10 に答える 10

75

Button のマージンの代わりに RelativeLayout にパディングを追加するだけですandroid:paddingBottom="15dp"

一般に、私は常に API レベル 8 設定を使用して Exclipse プレビューでレイアウトをテストしています。これにより、ICS や JB を含むほとんどのデバイスで非常に正確な結果が得られます。

于 2012-11-18T15:55:55.580 に答える
34

他にできることはView、 の下部に配置された を置き、RelativeLayoutその高さを使用したい (または単純に の値を指定するlayout_marginBottom) 下マージンに設定することです。

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    > 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/some_image"
        android:adjustViewBounds="true"

         />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some Overlay"
        android:padding="3dip"
        android:gravity="center_vertical|center_horizontal"

        android:layout_above="@+id/empty_view"
        android:layout_marginBottom="35dip"
        />
    <View 
        android:id = "@+id/empty_view"
        android:layout_height = "30dip"
        android:layout_width = "match_parent"
        android:visibility="invisible"
        android:layout_alignParentBottom="true"
        />

    </RelativeLayout>

この例では、 を で埋め、RelativeLayoutを の上ImageViewに配置TextViewImageViewます。

于 2013-09-19T04:18:32.640 に答える
20

TranslateY 属性を使用できます

translateY="-16dp"

最終コード

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="0dp"
    android:background="@drawable/background">

    <Button
        android:id="@+id/confirm_mobile_button_next"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="false"
        android:layout_margin="15dp"
        android:background="@drawable/button_shape_selector"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:text="@string/confirm_mobile_no_continue"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:translateY="-16dp" />

</RelativeLayout>
于 2016-07-21T09:07:20.517 に答える
2

最良の方法は次のように設定することだと思います:

<...
android:layout_alignParentBottom="true" />

次に、Javaコードビハインドで:

Button confirm_mobile_button_next = (Button)findViewById(R.id.confirm_mobile_button_next)
confirm_mobile_button_next.setTransitionY(-THE_VALUE) or setTransitionX(-THE_VALUE)
于 2015-01-09T14:25:21.123 に答える
-1

この方法で試してください:

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="17dp"
    android:text="Button" />

于 2012-11-18T16:00:02.293 に答える
-3

ここに別の選択肢があります。親のパディングではなく子のマージンを設定する場合は、 の値をandroid:layout_marginTop必要なマージンの 2 倍に設定してから、android:layout_centerVerticalを true に設定する必要があります。上余白には、下余白を補うために必要な値の 2 倍が与えられます。そうすれば、子ビューの上下の余白が等しくなります。

<Button
    android:id="@+id/confirm_mobile_button_next"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="false"
    android:layout_centerVertical="true"
    android:layout_centerInParent="false"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/button_shape_selector"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:text="@string/confirm_mobile_no_continue"
    android:textColor="@color/white"
    android:textStyle="bold" />

同じ結果が得られます。

于 2013-05-21T18:06:27.080 に答える