zoukankan      html  css  js  c++  java
  • 布局的居中问题

    首先区分一下gravity和layout_gravity:

    gravity:内部元素的对齐方式。

    layout_gravity:本元素相对于父布局的对齐方式。

    LinearLayout:

    相对于父布局居中,在父布局的LinearLayout设置:android:gravity="center"。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
    </LinearLayout>
    View Code

    相对于父布局水平居中,子布局设置: android:layout_gravity="center_horizontal" ,父布局设置:android:orientation="vertical",android:layout_width="match_parent"。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/hello_world" />
    
    </LinearLayout>
    View Code

    相对于父布局垂直居中,子布局设置:android:layout_gravity="center_vertical",父布局设置:

    android:orientation="horizontal" ,android:layout_height="match_parent"。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/hello_world" />
    
    </LinearLayout>
    View Code

    RelativeLayout:

    相对于父布局居中,在父布局的RelativeLayout设置:android:gravity="center"。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
    </RelativeLayout>
    View Code

    相对于父布局居中,在子布局设置: android:layout_centerInParent="true"。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:layout_centerInParent="true" />
    
    </RelativeLayout>
    View Code

    相对于父布局水平居中和垂直居中对应于:android:layout_centerHorizontal="true",android:layout_centerVertical="true"。

  • 相关阅读:
    linux 程序无缘无故推出 没有core文件 broken pipe Resource temporarily unavailable
    inline元素、block元素、inline-block元素
    转:Linux环境变量设置方法总结 PATH、LD_LIBRARY_PATH
    转: 深入理解信号槽机制
    【模式识别与机器学习】——PART2 机器学习——统计学习基础——Regularized Linear Regression
    【模式识别与机器学习】——PART2 机器学习——统计学习基础——2.Bias variance tradeoff
    关于GPU你必须知道的基本知识
    【计算机算法设计与分析】——5.5 0/1背包问题
    【模式识别与机器学习】——5.2 形式语言理论和句法模式识别
    【模式识别与机器学习】——句法模式识别5.1集合论中的关系运算
  • 原文地址:https://www.cnblogs.com/silenceshining/p/4691804.html
Copyright © 2011-2022 走看看