方法1:利用android:layout_weight

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hello_world" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world2" />
</LinearLayout>

hello_world2的宽度会挤压hello_world的空间。

效果图:

方法2:设置LinearLayout属性  android:gravity="right"

注:只有在LinearLayout的layout_width="match_parent"才有效。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world2" />
</LinearLayout>
  

效果图:

方法三: 从右到左布局(RTL Layout)

从Android 4.2开始,Android SDK支持一种从右到左(RTL,Right-to-Left)UI布局的方式,尽管这种布局方式经常被使用在诸如阿拉伯语、希伯来语等环境中,中国用户很少使用。不过在某些特殊用途中还是很方便的。

所谓RTL,就是指按平常习惯在左的视图都会在右侧,在右侧的视图都会在左侧。例如,在线性布局中第1个子视图默认都是在左上角的,如果采用RTL布局,默认就在右上角了。

RTL布局默认是关闭的,如果想使用RTL布局,首先要在AndroidManifest.xml文件中将<application>标签的android:supportsRtl属性值设为"true",然后需要将相应视图标签的android:layoutDirection属性值设为"rtl"。

注意:RTL布局常用android:layout_marginStart和android:layout_marginEnd来设置两个视图的间距。

android:layout_marginStart:如果在LTR布局模式下,该属性等同于android:layout_marginLeft。如果在RTL布局模式下,该属性等同于android:layout_marginRight。

android:layout_marginEnd:如果在LTR布局模式下,该属性等同于android:layout_marginRight。如果在RTL布局模式下,该属性等同于android:layout_marginLeft。

例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F00"
android:text="TextView1"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:background="#F00"
android:text="TextView2"
android:textSize="30sp" />
</LinearLayout>

 效果如下:

[android]如何使LinearLayout布局从右向左水平排列,而不是从左向右排列的更多相关文章

  1. Android中的LinearLayout布局

    LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了,  线性布局是按照垂直方向(vertical)或水平方向 ...

  2. [Android学习笔记]LinearLayout布局,剩余空间的使用

    转自:http://segmentfault.com/q/1010000000095725 如果使得一个View占用其父View的剩余空间? 答案是使用:android:layout_weight = ...

  3. Android教程-03 常见布局的总结

    常见的布局 视频建议采用超清模式观看, 欢迎点击订阅我的优酷 Android的图形用户界面是由多个View和ViewGroup构建出来的.View是通用的UI窗体小组件,比如按钮(Button)或者文 ...

  4. android中的常用布局管理器(三)

    接上篇博客 (5)TableLayout     表格布局管理器 在android中,线性布局和表格布局用的是最多的. 在很多的输出操作中,往往会使用表格的形式对显示的数据进行排版,tablelayo ...

  5. Android学习——LinearLayout布局实现居中、左对齐、右对齐

    android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...

  6. Android四种基本布局(LinearLayout \ RelativeLayout \ FrameLayout \ TableLayout)

    ------------------------------------------LinearLayout---------------------------------------------- ...

  7. 15.Android中LinearLayout布局一些小记录

    在App中,我们经常看到布局中会有分割线,直接上代码: <?xml version="1.0" encoding="utf-8"?> <Lin ...

  8. Android布局_布局概述和LinearLayout布局

    一.布局概述: 布局为UI提供了一个可视化的结构,比如对于一个activity或者app widget的UI,你可以用两种方式声明布局: 在XML中声明UI元素 在运行时实例化布局元素(在java代码 ...

  9. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

随机推荐

  1. C语言内存管理(转)

    伟大的Bill Gates 曾经失言: 640K ought to be enough for everybody — Bill Gates 1981 程序员们经常编写内存管理程序,往往提心吊胆.如果 ...

  2. c#winform选择文件,文件夹,打开指定目录方法

    private void btnFile_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDia ...

  3. .net利用NPOI导入导出Excel

    NPOI在.net中的操作Excel 1.读取 using (FileStream stream = new FileStream(@"c:\客户资料.xls", FileMode ...

  4. nyoj 252 01串 动态规划( java)

    当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...

  5. r8 - ASC 41(俄罗斯多校)

    1 今天干的俄罗斯的一场多校,被虐哭啊,就做出两题. 2 3 4 5 6 7 Gym 100496D Data Mining 8 题目讲得是给你一串数字,然后给你i,p,表示从第i开始,对这串数离散话 ...

  6. ICloud没有密码怎么注销?

    忘了密码的用这方法就可以啦1.现在我说你做 . 先进入 iCloud 点击 某某账户(要删的账户)2.第二栏密码那里删除原来的再输入ios(任意密码)3.然后点完成 之后会出现错误.请点 好.然后左上 ...

  7. Why Reflection is slowly?(Trail: The Reflection API)

    反射的使用 反射通常用于在JVM中应用程序运行中需要检查或者修改运行时行为的项目.这是一个相对高级的特性,并且仅仅可以被对深刻理解java原理的开发者使用.这里给出一个警告的意见,反射是一个强大的技术 ...

  8. 安装JBPM6运行环境(JBPM6学习之二)

    安装Eclipse插件成功后,需要配置JBPM6的运行环境: 1. 第一步先将下载的jbpm6目录中的“jbpm-6.0.1.Final-bin.zip”找到,并解压缩到D盘根目录备用. 2. 第二步 ...

  9. PC-BSD 换名 TrueOS

    导读 PC-BSD 重大更新:面向私人电脑的PC-BSD操作系统开发者.Lumina桌面环境创造者Ken Moore今天宣布系统迎来重大改变. 首先,PC-BSD更名为TrueOS,用户依然能够在PC ...

  10. python 函数式编程工具

    有三个内置函数与列表一起使用时非常有用:filter().map()和reduce(). 1. filter(function, sequence)返回的序列由function(item)结果为真的元 ...