zoukankan      html  css  js  c++  java
  • LinearLayout中组件右对齐

    在LinearLayout中,如果将其定位方向设为横向排列:android:orientation="horizontal",那么这个布局中的控件将自左向右排列。

    但有时会有这样的情况:行的左边有两个控制的同时,行的右边也有一个控制。

    如图:

    这怎么处理呢?

    我们可以将右边的控件放在另一个LinearLayout中,同时将其对齐方式设为右对齐:android:gravity="right",还有一点,这个LinearLayout的宽度设为充满父控件: android:layout_width="fill_parent"。这样就行了。

    完整的XML代码如下:

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:background="@drawable/bg"  
    6.     android:orientation="horizontal" >  
    7.   
    8.     <TextView  
    9.         android:layout_width="wrap_content"  
    10.         android:layout_height="wrap_content"  
    11.         android:text="左边1" />  
    12.   
    13.     <TextView  
    14.         android:layout_width="wrap_content"  
    15.         android:layout_height="wrap_content"  
    16.         android:text="左边2" />  
    17.     <!-- 将TextView包在另一个LinearLayout中  
    18.          注意android:layout_width和android:gravity这两个属性  
    19.      -->  
    20.     <LinearLayout  
    21.         android:layout_width="fill_parent"  
    22.         android:layout_height="wrap_content"  
    23.         android:gravity="right" >  
    24.   
    25.         <TextView  
    26.             android:layout_width="wrap_content"  
    27.             android:layout_height="wrap_content"  
    28.             android:layout_marginRight="10dp"  
    29.             android:text="右边" />  
    30.     </LinearLayout>  
    31.   
    32. </LinearLayout>  

  • 相关阅读:
    有7g和2g的砝码各一个,怎样称可以3次把140g东西分为50g和90g???????
    中缀到后缀(一个例子)
    动态代理模式的使用
    代理模式用来初始化的延迟下载
    ReentrantLock Condition 实现消费者生产者问题
    Two Sum
    [leetcode]重建二叉树(先序和终须) 中序遍和后续
    (转载)旋转数组查找 最简洁方法 总结
    [不明觉厉] 下一个排列
    codeforces -- 283A
  • 原文地址:https://www.cnblogs.com/chengliu/p/4130604.html
Copyright © 2011-2022 走看看