zoukankan      html  css  js  c++  java
  • [android]如何使LinearLayout布局从右向左水平排列,而不是从左向右排列

    方法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> 
    

     效果如下:

  • 相关阅读:
    mysql增量同步到greenplum
    c笔记06--编译与作用域
    C笔记05-选择顺序结构,关系与相等,优先级和结合性
    C笔记02-C数据类型与数据类型转换
    C笔记01-C简介与补码
    jQuery属性操作之.val()函数
    jQuery属性操作之.attr()
    jQuery笔记: 基本概念与jQuery核心
    笔记: js构造函数与原型
    布尔运算符
  • 原文地址:https://www.cnblogs.com/ryq2014/p/5180214.html
Copyright © 2011-2022 走看看