第八篇 掌握布局 之 LinearLayout

1.使用LinearLayout
android:orientation=”horizontal” // 水平排列
android:orientation=”vertical” // 垂直排列

android:weight = “number” //number是 数字,weight表示分隔父级容器

样式一、
这里写图片描述

<?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"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <TextView
        android:layout_weight="1"
        android:background="#ff0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_weight="1"
        android:background="#f0f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_weight="1"
        android:background="#f00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">
<!--占据剩余空间-->
    <TextView
        android:layout_weight="1"
        android:text="hello wife"
        android:background="#ff0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <!--适应内容-->
    <TextView
        android:text="hello xiyouji"
        android:background="#f0f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

3.样式三
简单的仿浏览器
这里写图片描述

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜索"/>
    </LinearLayout>
    <WebView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"></WebView>
</LinearLayout>

第二部分:
使用代码控制样式
这里写图片描述

package com.wang.learnlinearlayout;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private LinearLayout linearLayout;
    private Button btnOne;
    private Button btnTwo;
    private Button btnThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);

        btnOne = new Button(this);
        btnOne.setText("one");
        btnTwo = new Button(this);
        btnTwo.setText("two");
        btnThree = new Button(this);
        btnThree.setText("three");
        //第一种   自定义宽、高
        linearLayout.addView(btnOne,300,100);
        //第二种   使用系统常量
        linearLayout.addView(btnTwo,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

        //第三种声明布局参数,根据布局参数添加子对象
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.addView(btnThree,lp);

        //实现4个按钮,分割父级容器,占满剩余空间,然后点击这些按钮,都会被删除

        for (int i = 0;i < 4;i ++){
            Button btnOther = new Button(this);
            btnOther.setText("item"+i);
            //点击按钮,就会被删除
            btnOther.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    linearLayout.removeView(v);
                }
            });
            LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.weight = 1;
            linearLayout.addView(btnOther,layout);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值