[英]Android Layout left and right align in horizontal layout
我有一個 ImageView 和一個 ImageButton。 我讓它們在水平布局中彼此相鄰。 我試圖使圖像在屏幕上左對齊,按鈕右對齊。 我試過設置重力,但似乎沒有什么區別。 我哪里錯了?
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:gravity="left"
android:src="@drawable/short_logo" />
<ImageButton
android:id="@+id/terminateSeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:background="@null"
android:src="@drawable/unregister_icon" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
采用...
<?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="match_parent"
android:orientation="horizontal"
android:weightSum="1.0" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="80dp"
android:layout_weight="0.5"
android:gravity="left"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/terminateSeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="right"
android:layout_marginLeft="80dp"
android:background="@null"
android:src="@drawable/ic_launcher" />
</LinearLayout>
如果您想使用 LinearLayout 而不提供 Weight,請使用此示例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
/>
</LinearLayout>
<RelativeLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_alignParentLeft="true"
android:src="@drawable/short_logo" />
<ImageButton
android:id="@+id/terminateSeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@null"
android:src="@drawable/unregister_icon" />
</RelativeLayout >
制作imageview
和imagebutton
wrap_content
並相應地將layout_gravity
設置為左右。
一個RelativeLayout
可以快速解決你的問題,但如果你仍然堅持使用 LinearLayout(僅僅因為我們是壞蛋並且做事很困難),你可以這樣做:
您的父 LinearLayout 是填充父級、左對齊和水平方向,並包含您的 ImageView 和另一個 LinearLayout。
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
第二個 LinearLayout 是父級填充、右對齊和水平方向,並包含您的 ImageButton。
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
LinearLayout
另一種方法,使用layout_weight
和gravity
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="BUTTON 1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="BUTTON 2" />
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.