簡體   English   中英

如何在線性布局中固定按鈕位置

[英]How to fix the button position in Linear Layout

我在線性布局中定義了兩個組件(一個文本視圖和一個按鈕)。

<LinearLayout 
android:id="@+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/edit_view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize = "15sp"
    android:layout_weight="5"
    android:layout_marginTop="20dp"
    android:layout_alignParentLeft="true"
    android:gravity="center"
    android:hint="Write Greeting" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_toRightOf="@+id/edit_view1"
    android:layout_alignParentRight="true"
    android:background="@android:color/transparent"
    android:src="@drawable/e301"/>
</LinearLayout>

如果我寫長文本,則ImageButton的位置會更改。 我要固定這個位置。 有什么建議嗎?

將布局內容替換為:

<LinearLayout 
android:id="@+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/edit_view1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize = "15sp"
    android:layout_weight="5"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:hint="Write Greeting" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/transparent"
    android:src="@drawable/e301"/>
</LinearLayout>

您不能在線性布局內使用RelativeLayout的對齊屬性。

查看修改后的代碼:

<LinearLayout 
android:id="@+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:singleLine="false"
    android:id="@+id/edit_view1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize = "15sp"
    android:layout_marginTop="20dp"
    android:text="etegtdrgrgbvcvbghgfffffffffffffffffffffffffffff"
    android:gravity="center"
    android:hint="Write Greeting" 
    android:layout_weight="2"/>

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:layout_weight="1"

    android:background="@android:color/transparent"
    android:src="@drawable/ic_launcher"
    />
</LinearLayout>

使用weight和weightSum保持布局寬度為0 dp,以便進行調整。 另外,對於文本視圖,將sengleLine保留為false。

輸出:

在此處輸入圖片說明

希望這可以幫助。

試試下面的代碼:

<LinearLayout
    android:id="@+id/layout_1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/edit_view1"
        android:layout_width="0dp"
        android:layout_weight="5"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:hint="Write Greeting"
        android:textSize="15sp" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/edit_view1"
        android:background="@android:color/transparent"
        android:src="@drawable/e301" />
</LinearLayout>

使用重量,則寬度必須為0dp(水平),高度為0dp(垂直)

  1. 使用權重:您沒有定義weightSum布局的weightSum ,因此您的權重現在是無用的。 將父級布局的weightSum設置為6(以便使用您的值)或100,並將子級的權重設置為百分比。 並將子視圖的寬度更改為0dp

  2. [可選]將按鈕的寬度設置為match_parent這樣它將占用所有剩余空間。

  3. 在LinearLayout layout_toRightOf,layout_alignParentRight,layout_alignParentLeft中是多余的。 請改為使用gravity作為兒童視圖。 (對於Button,它將為'gravity =“ right”)。

使用以下代碼。 告訴我是否要更好地對齊。 我已使用默認默認圖像進行對齊。 如果您想要比這更好的話,那么也要提供按鈕圖像。

<LinearLayout
        android:id="@+id/layout_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/edit_view1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:hint="Write Greeting dfdsfdsfsdfadsfdfadf"
                android:textSize="15sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3" >

            <ImageButton
                android:id="@+id/imageButton1"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_launcher" />
        </RelativeLayout>
    </LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM