[英]How to fix the button position in Linear Layout
I define two components (One text View and one Button) 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>
If I write long text, the position of my ImageButton
is changed. 如果我写长文本,则ImageButton
的位置会更改。 I want to fix the position. 我要固定这个位置。 Any suggestion? 有什么建议吗?
Replace your layout contents as : 将布局内容替换为:
<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>
You can't use RelativeLayout's properties of alignment inside Linear layout. 您不能在线性布局内使用RelativeLayout的对齐属性。
See your modified code: 查看修改后的代码:
<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>
Keep layout width as 0 dp using weight and weightSum, so it will adjust. 使用weight和weightSum保持布局宽度为0 dp,以便进行调整。 Also, keep sengleLine as false for textview. 另外,对于文本视图,将sengleLine保留为false。
Output: 输出:
Hope this helps. 希望这可以帮助。
Try below code:- 试试下面的代码:
<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>
Using weight then you must have width = 0dp(horizontal) and heigth = 0dp(vertically) 使用重量,则宽度必须为0dp(水平),高度为0dp(垂直)
Use weights: you didn't define weightSum
of layout so your weights are now useless. 使用权重:您没有定义weightSum
布局的weightSum
,因此您的权重现在是无用的。 Set parent layout's weightSum
to 6 (in order to use your values) or 100 and set children's weights as percents. 将父级布局的weightSum
设置为6(以便使用您的值)或100,并将子级的权重设置为百分比。 And change children views' widths to 0dp
. 并将子视图的宽度更改为0dp
。
[optional] Set button's width to match_parent
so it'll take all remaining space. [可选]将按钮的宽度设置为match_parent
这样它将占用所有剩余空间。
In LinearLayout layout_toRightOf, layout_alignParentRight, layout_alignParentLeft are redundant. 在LinearLayout layout_toRightOf,layout_alignParentRight,layout_alignParentLeft中是多余的。 Use gravity
for children views instead. 请改为使用gravity
作为儿童视图。 (for Button it'll be `gravity="right"). (对于Button,它将为'gravity =“ right”)。
Use the below coding. 使用以下代码。 Tell me if you want better alignment. 告诉我是否要更好地对齐。 I have used default default image for alignment. 我已使用默认默认图像进行对齐。 If you want better than this, then give buttom image also. 如果您想要比这更好的话,那么也要提供按钮图像。
<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.