7

水平方向の線形レイアウトでテキストビューとボタンを作成しようとしています。テキストビューは最初に表示され、ボタンは最後に表示されます。ボタンに重力を与えるとうまくいくと思ったのですが、ボタンが右側に移動しません。おそらく相対レイアウトを使用する必要があるかどうかを考えていますか?

ここに画像の説明を入力

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

    <TextView
        android:id="@+id/productPriceTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rs 3579.0" 
        />

    <Button
        android:id="@+id/buyNowButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Buy Now" />
<\/LinearLayout>
4

8 に答える 8

8

以下の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="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/productPriceTextView1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Rs 3579.0"
        />

    <Button
        android:id="@+id/buyNowButton1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Buy Now" />
</LinearLayout>
于 2014-08-21T09:12:17.893 に答える
1

2 つのアプローチがあります。

1)好きな場所にRelativeLayoutドラッグできるButton場所を使用できます..

2) には weight プロパティを使用できますLinearLayout

<?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="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/productPriceTextView1"
    android:layout_width="0dp"
    android:layout_weight="0.8"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:text="Rs 3579.0"
    />

<Button
    android:id="@+id/buyNowButton1"
    android:layout_width="0dp"
    android:layout_weight="0.2"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="Buy Now" />
</LinearLayout>
于 2014-08-21T09:13:31.440 に答える
1

代わりにこのレイアウトを使用してください。

<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="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs 3579.0"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />



<Button
android:id="@+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Now"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>
于 2014-08-21T09:16:35.663 に答える
0

android:layout_gravity="end"線形レイアウトを使用して、すべての要素を右に移動します

于 2017-09-14T07:42:33.840 に答える