如何将重力应用于线性布局中方向为水平的项目?
How to apply gravity to the items in Linear Layout, whose orientation is Horizontal?
我有一个父线性布局,其方向为 vertical
,在此之下我有另一个布局,其方向为 vertical
,它有一个子布局,其方向为 Horizontal
.
此布局有 2 个 Image Views
。 ,我想对这些图像 views
.1 和 left-gravity
以及 1 right-gravity
.
应用引力
当我将重力应用到此布局时,它不会产生任何效果,但如果我删除最后一个 horizontal
布局,它会完美地工作,但显示 1 个图像向上和 1 个向下。
这是我的代码,请指导我
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b9000000"
android:id="@+id/ll_black_overlay"
>
//There are 2 More layouts, whith weights 20 and 50
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="70dp"
android:paddingRight="70dp"
>
// Layout for displaying text messages Vertically
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View 1 , "
android:textColor="@color/white"
android:layout_gravity="center"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View 002 , "
android:textColor="@color/white"
android:layout_gravity="center"
android:gravity="center"
/>
</LinearLayout>
// --Problem area Layout for displaying Images side by side,
1 image at left corner, 1 at right corner, if i remove Linear layout,
gravity works, but id is not working with Linear Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icn_tour_arrow_left"
android:layout_gravity="left"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icn_tour_arrow_right"
android:layout_gravity="right"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
使用下面的布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_black_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b9000000"
android:orientation="vertical">
//There are 2 More layouts, whith weights 20 and 50
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="vertical"
android:paddingLeft="70dp"
android:paddingRight="70dp"
android:paddingTop="20dp">
// Layout for displaying text messages Vertically
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Text View 1 , "
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Text View 002 , "
android:textColor="@color/white" />
</LinearLayout>
// --Problem area Layout for displaying Images side by side,
1 image at left corner, 1 at right corner, if i remove Linear layout,
gravity works, but id is not working with Linear Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/icn_tour_arrow_left"
android:layout_gravity="left"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/icn_tour_arrow_right"
android:layout_gravity="right"
/>
</LinearLayout>
它会在屏幕上显示两个相等的图像
如果您希望两个图像都在角落,请在 layout_weight
的两个图像之间添加另一个文本视图
Just add LinearLayout with weight 1 in between ImageViews.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icn_tour_arrow_left"
android:layout_gravity="left"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icn_tour_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_black_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b9000000"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Text View 1 , "
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Text View 002 , "
android:textColor="@color/white" />
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout"
android:src="@drawable/icn_tour_arrow_right" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_gravity="right"
android:layout_toLeftOf="@+id/imageView"
android:layout_toStartOf="@+id/imageView"
android:src="@drawable/icn_tour_arrow_left"/>
我有一个父线性布局,其方向为 vertical
,在此之下我有另一个布局,其方向为 vertical
,它有一个子布局,其方向为 Horizontal
.
此布局有 2 个 Image Views
。 ,我想对这些图像 views
.1 和 left-gravity
以及 1 right-gravity
.
当我将重力应用到此布局时,它不会产生任何效果,但如果我删除最后一个 horizontal
布局,它会完美地工作,但显示 1 个图像向上和 1 个向下。
这是我的代码,请指导我
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b9000000"
android:id="@+id/ll_black_overlay"
>
//There are 2 More layouts, whith weights 20 and 50
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="70dp"
android:paddingRight="70dp"
>
// Layout for displaying text messages Vertically
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View 1 , "
android:textColor="@color/white"
android:layout_gravity="center"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View 002 , "
android:textColor="@color/white"
android:layout_gravity="center"
android:gravity="center"
/>
</LinearLayout>
// --Problem area Layout for displaying Images side by side,
1 image at left corner, 1 at right corner, if i remove Linear layout,
gravity works, but id is not working with Linear Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icn_tour_arrow_left"
android:layout_gravity="left"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icn_tour_arrow_right"
android:layout_gravity="right"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
使用下面的布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_black_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b9000000"
android:orientation="vertical">
//There are 2 More layouts, whith weights 20 and 50
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="vertical"
android:paddingLeft="70dp"
android:paddingRight="70dp"
android:paddingTop="20dp">
// Layout for displaying text messages Vertically
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Text View 1 , "
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Text View 002 , "
android:textColor="@color/white" />
</LinearLayout>
// --Problem area Layout for displaying Images side by side,
1 image at left corner, 1 at right corner, if i remove Linear layout,
gravity works, but id is not working with Linear Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/icn_tour_arrow_left"
android:layout_gravity="left"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/icn_tour_arrow_right"
android:layout_gravity="right"
/>
</LinearLayout>
它会在屏幕上显示两个相等的图像 如果您希望两个图像都在角落,请在 layout_weight
的两个图像之间添加另一个文本视图Just add LinearLayout with weight 1 in between ImageViews.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icn_tour_arrow_left"
android:layout_gravity="left"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icn_tour_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_black_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b9000000"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Text View 1 , "
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Text View 002 , "
android:textColor="@color/white" />
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout"
android:src="@drawable/icn_tour_arrow_right" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_gravity="right"
android:layout_toLeftOf="@+id/imageView"
android:layout_toStartOf="@+id/imageView"
android:src="@drawable/icn_tour_arrow_left"/>