在LinearLayout中,如果将其定位方向设为横向排列:android:orientation="horizontal",那么这个布局中的控件将自左向右排列。
但有时会有这样的情况:行的左边有两个控制的同时,行的右边也有一个控制。
这怎么处理呢?
我们可以将右边的控件放在另一个LinearLayout中,同时将其对齐方式设为右对齐:android:gravity="right",还有一点,这个LinearLayout的宽度设为充满父控件: android:layout_width="fill_parent"。这样就行了。
完整的XML代码如下:
Xml代码
-
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
-
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
-
android:layout_width
=
"fill_parent"
-
android:layout_height
=
"fill_parent"
-
android:background
=
"@drawable/bg"
-
android:orientation
=
"horizontal"
>
-
-
<
TextView
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:text
=
"左边1"
/>
-
-
<
TextView
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:text
=
"左边2"
/>
-
<!-- 将TextView包在另一个LinearLayout中
-
注意android:layout_width和android:gravity这两个属性
-
--
>
-
<
LinearLayout
-
android:layout_width
=
"fill_parent"
-
android:layout_height
=
"wrap_content"
-
android:gravity
=
"right"
>
-
-
<
TextView
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:layout_marginRight
=
"10dp"
-
android:text
=
"右边"
/>
-
</
LinearLayout
>
-
-
</
LinearLayout
>
在LinearLayout中,如果将其定位方向设为横向排列:android:orientation="horizontal",那么这个布局中的控件将自左向右排列。但有时会有这样的情况:行的左边有两个控制的同时,行的右边也有一个控制。如图:这怎么处理呢?我们可以将右边的控件放在另一个LinearLayout中,同时将其对齐方式设为右对齐:android:gravity
gravity:控制
组件
所包含的子元素的对齐方式,可多个组合。如(left | button)
layout_gravity:控制该
组件
在父元素里的对齐方式。
开始使用studio实验吧:
1.给布局使用gravity属性:
flex布局
中
的一个子元素单独右侧对齐
我们可能会在 `Flexbox`容器
中
使用 `justify-self` 属性来实现子元素的水平对齐。但是,需要注意的是,`justify-self` 在 `Flex`布局
中
是无效的。
那么常见的做法是将子元素的 `margin-left` 设置为 `auto`。
在
LinearLayout
中
,如果将其定位方向设为横向排列:android:orientation="horizontal",那么这个布局
中
的控件将自左向右排列。
但有时会有这样的情况:行的左边有两个控制的同时,行的右边也有一个控制。
这怎么处理呢?
我们可以将右边的控件放在另一个
LinearLayout
中
,同时将其对齐方式设为
右对齐
:android:gravity
<?xml version="1.0" encoding="utf-8"?>
<
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/activity_main"
android:layout_width="match_parent"
转自:http://www.cnblogs.com/xiaoran1129/archive/2013/03/26/2982954.html
linearlayout
水平靠右显示方法
在安卓
中
,只有垂直的
linearlayout
属性里面的内部
组件
靠左靠右才有效,同理,水平的
linearlayout
属性里面的内部
组件
靠上靠下才有效,否则无效由此考虑如何让水平
linearlayout
中
同一行内组
我根布局用的
LinearLayout
;
然后嵌套了一个一定高度的
LinearLayout
;为上图顶部宽度撑满父元素的矩形框;称之为“线布2”;
线布2 里又水平布局了两个线性布局;
左边的线性布局不管; 右边的线性布局里嵌套了4个button按钮;
现在的问题是:
超出 线布2 的3个button按钮无法显示;
超出 线布2 的3个button按钮无法响应点击事件