zoukankan      html  css  js  c++  java
  • android五大布局居中对齐方式

    1.LinearLayout(线性布局)

    如果是要把imagebutton之类的控件居中对齐的话,要用android:layout_gravity  代码如下:

     <LinearLayout
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal"
                 android:layout_gravity="center_horizontal"
                 >
    
                 <ImageView
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     android:layout_marginTop="20dp"
                     android:src="@drawable/ic_mytce" />
    
             </LinearLayout>

    如果是textview之类文本控件就不能像上面那样设置了,要用android:gravity="center",  这个用于文本对齐

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    
     
    
    <TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:text="2"/>
    
     
    
    </RelativeLayout>

    5.TableLayout(表格布局)

    暂时没使用过,就不说了。

                <LinearLayout
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal"
                 
                 >
                 <TextView
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     android:gravity="center"
                     android:layout_marginTop="8dp"
                     android:textColor="#3A3C3B"
                     android:textSize="14dp"
                     android:text="版本号1.0"
                    />
    
             </LinearLayout>

    2.FrameLayout(单帧布局)

    因为它的特证就是,所有的子元素都只会显示窗口的左上角,而且下一个子元素会覆盖上一个子元素,所以理论上控件是不可以居中的;只有在textview文本控件的情况下可文本局中;

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    
        <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/>
    
     
    
    </FrameLayout>

    3.AbsoluteLayout(绝对布局)

    它要定位就要确定绝对的x,y的坐标,如果你能获取到你手机屏幕的像素值,它用起来还是很好的

    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    
      
    
        <TextView  android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba"
    
    android:gravity="center" android:layout_x="125dp" android:layout_y="125dp" android:text="3"/>
    
    
    </AbsoluteLayout>

    4.RelativeLayout(相对布局)
    这个我个人很喜欢,很灵活,用得也很多;它指定居中非常简单,直接android:layout_centerHorizontal="true"就行了

    Crazy Cherry:everything is possible!
  • 相关阅读:
    Makoto and a Blackboard CodeForces
    Bash Plays with Functions CodeForces
    2016 计蒜之道 初赛 第一场 D 青云的机房组网方案 (虚树)
    常用数论函数求和公式
    AC日记——元素查找 codevs 1230
    AC日记——鬼谷子的钱袋 codevs 2998
    AC日记——接龙游戏 codevs 1051
    AC日记——逆波兰表达式 openjudge 3.3 1696
    AC日记——欧几里得的游戏 洛谷 P1290
    AC日记——刺激 codevs 1958
  • 原文地址:https://www.cnblogs.com/userbibi/p/2516666.html
Copyright © 2011-2022 走看看