Android基础知识

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

2018-06-11  本文已影响32人  野岗狼沟兜

控件所占的空间和其 内容 所占空间的区别

关于控件的基本认识

image

1. match_parent 填充自己父一层布局空间,简单理解就是外一层容器你给我多大,我就能多大

2. wrap_content 我的大小是根据我所装的东西的大小来的,简单理解就是呈现的内容多大,我就有多大

3. 固定值比如 48dp

image
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#ff0000">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一个TextView"
        android:background="#0000ff"
        android:textColor="#ffffff"
       />
</LinearLayout>

那个 控件空间 傻傻分不清,嗯,这是搜狗输入法的锅,大家读的时候注意下,我发现很多地方都是错乱的……

image
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#ff0000">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一个TextView第一个TextView第一个TextView第一个TextView第一个TextView第一个TextView第一个TextView第一个TextView第一个TextView第一个TextView第一个TextView第一个TextView"
        android:background="#0000ff"
        android:textColor="#ffffff"
       />
</LinearLayout>

看看效果:

image
  1. 用于显示“第一个TextView”的控件,即TextView必须占据右下角(也即是蓝色必须先到达右下角)
  2. TextView想要占据右下角,控件TextView的外一层控件,即Linearlayout必须占据右下角((这是因为TextView是“装在”Linearlayout里面的)也即是红色必须先到达右下角)
    结合图形我们易知,第二个条件已经达成,那么在此条件下,怎么利用属性 layout_gravity和属性 gravity达到我们目标呢,Android中将控件放到线性布局的任意位置(二)我们就就讲这两个属性。
上一篇 下一篇

猜你喜欢

热点阅读