Android基础知识

Android中将控件放到线性布局的任意位置(三)

2018-06-12  本文已影响17人  野岗狼沟兜

orientationlayout_gravity之间鱼和熊掌的关系?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:orientation="vertical"
    tools:context=".MainActivity"
   >

    <TextView
        android:textSize="25sp"
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0000ff"
        android:text="第一个TextView"
        android:textColor="#ffffff"
        android:layout_gravity="right"
        />
</LinearLayout>
  1. 在所有布局中,默认情况下,它的子控件会从父控件的左上角开始布局(这个方向,和计算机领域,屏幕的方向是相同的,从左上角开始,向右为X正方向,向下为Y轴正方向,左上角坐标为(0,0),相应的,右下角为(maxWidth,maxHeight)(两者分别代表屏幕最大宽度、高度))
  2. 线性布局(LinearLayout),顾名思义,它的子控件会按照线性方向(X或Y方向)布局,至于是向X(水平)方向还是(竖直)方向布局,则就是有android:orientation指定的,默认情况下,线性布局布局会按照水平方向布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
tools:context=".MainActivity"
    >
<TextView
    android:textSize="25sp"
    android:id="@+id/tv_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#0000ff"
    android:text="第一个TextView"
    android:textColor="#ffffff"
    android:layout_gravity="right"
    />
    <TextView
        android:textSize="25sp"
        android:id="@+id/tv_tow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#555555"
        android:text="第二个TextView"
        android:textColor="#ffffff"
        android:layout_gravity="right"
        />
</LinearLayout>
  1. 父布局:android:orientation="horizontal",子控件:android:layout_gravity="bottom"
  2. 父布局:android:orientation="vertical",子控件:android:layout_gravity="right"
    会生效吗,我们试试:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    >

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#0000ff"
        android:text="第一个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_tow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#555555"
        android:text="第二个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />
</LinearLayout>
效果: 3.png

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#0000ff"
        android:text="第一个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_tow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#555555"
        android:text="第二个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />
</LinearLayout>
  1. 两个方向便能确定子控件在父控件的具体位置,父控件确定一个维度,子控件本事确定一个维度,逻辑清晰明了。
  2. 最为关键是,如果父控件和子控件都能在同一维度起作用的话,会有明显的冲突,比如布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#0000ff"
        android:text="第一个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_tow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#555555"
        android:text="第二个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />
</LinearLayout>
上一篇 下一篇

猜你喜欢

热点阅读