LinearLayout初步了解

来源:互联网 发布:魔兽世界7.0 数据库 编辑:程序博客网 时间:2024/05/29 18:39
布局:
    1、文件位置res/layout/***.xml
    2、确定布局管理器类型
    3、在布局管理器中添加需要的组件
    4、修改activity里指向的布局文件
    5、所有的资源文件都必须小写

LinearLayout:
    特点:每一行或者每一列中,只能放一个组件,当组件一个一个的排列到窗体的边缘后,剩下的组件不会被显示。在垂直线性布局管理器中,每一行只能放一个组件,在水平线性布局管理器中,每一列只能放一个组件。
    方式:XML配置或者JAVA代码 推荐XML配置
    常用属性:
        android:orientation="horizontal | vertical":vertical垂直的,horizontal水平的
        android:layout_width="wrap_content | match_parent": 包裹内容 | 填充父类空间
        android:layout_height="match-parent":同上(fill_parent不推荐使用)
        android:id="@+id/名称":设置id这个属性,可以在java文件中取到布局
        android:background:图片或者颜色
        android:gravity:控制布局控件中的对齐方式。如果没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式,若有子控件的控件设置此属性,表示其子控件的对其方式,gravity如果需要设置多个属性,中间用"|"进行组合。
        android:layout_weight="1":通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小,线性布局会根据该控件layout_weight值与其所处布局中的所有控件layout_weight值之和的比值为该控件分配占用的区域。如果layout_weight值为0,控件会按原大小显示,不会被拉伸,对于其他layout_weight属性值大于0的控件,系统将会减去属性值为0的元素所占位置的大小,然后剩余的按比例分配
    android:visibility=""visible | invisible | gone":显示 | 不显示但会占位置 |  不宣示也不占位置 

android:gravity与android:layout_gravity的区别:
    android:gravity:是指本元素的子元素相对它的对齐方式
    android:layout_gravity:是指本元素相对它的父元素的对齐方式。

android:layout_margin***:设置本控件距离父控件的个数
android:layout_padding***:设置子控件距离父控件的距离
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 
<LinearLayout
android:id="@+id/ll_up"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#ff00ff"
android:gravity="top"
android:orientation="horizontal"
android:padding="4dp" >
 
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="第一列" />
 
<Button
android:id="@+id/btn_close"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:gravity="bottom|center"
android:padding="5dp"
android:text="关闭" />
 
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="第二列" />
</LinearLayout>
 
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
 
</LinearLayout>
利用java代码实现相同效果,不推荐
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
 
public class MainActivity extends Activity {
 
private LinearLayout ll_parent;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 
// 获得父控件
ll_parent = (LinearLayout) findViewById(R.id.ll_parent);
 
// 新建新的LinearLayout控件
LinearLayout ll_up = new LinearLayout(this);
 
// 新建LinearLayout属性对象
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1);
 
// 添加属性
ll_up.setLayoutParams(params);
 
// 设置方向
ll_up.setOrientation(LinearLayout.HORIZONTAL);
 
// 添加图片资源
ll_up.setBackgroundResource(R.drawable.fuck);
 
// 添加按钮1
// 新建一个按钮
Button btn_01 = new Button(this);
// 设置按钮的字体
btn_01.setText("按钮1");
// 新建按钮的属性
LayoutParams params2 = new LayoutParams(150, LayoutParams.MATCH_PARENT);
// 把属性添加到按钮中
btn_01.setLayoutParams(params2);
// 把按钮添加到父控件中
ll_up.addView(btn_01);
 
// 添加按钮
Button btn_02 = new Button(this);
btn_02.setText("按钮2");
btn_02.setLayoutParams(params2);
ll_up.addView(btn_02);
 
// 添加按钮
Button btn_03 = new Button(this);
btn_03.setText("按钮3");
btn_03.setLayoutParams(params2);
ll_up.addView(btn_03);
 
// 父控件中添加这个控件
ll_parent.addView(ll_up);
 
LinearLayout ll_down = new LinearLayout(this);
ll_down.setLayoutParams(params);
ll_down.setBackgroundResource(R.drawable.lj0601bz11);
 
ll_down.setOrientation(LinearLayout.VERTICAL);
 
// 添加按钮
Button btn_04 = new Button(this);
btn_04.setText("按钮4");
// 这次用的是父类型的类型
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1);
btn_04.setLayoutParams(params4);
ll_down.addView(btn_04);
 
// 添加按钮
Button btn_05 = new Button(this);
btn_05.setText("按钮5");
btn_05.setLayoutParams(params4);
ll_down.addView(btn_05);
 
// 添加按钮
Button btn_06 = new Button(this);
btn_06.setText("按钮6");
btn_06.setLayoutParams(params4);
ll_down.addView(btn_06);
 
ll_parent.addView(ll_down);
 
}
 
}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 比亚迪s7噪音大怎么办 比亚迪f3噪音大怎么办 买房首付差10万怎么办 车贷合同没给我怎么办 车内老是有灰尘怎么办 车在北京怎么办进京证 五证合一后社保怎么办 五证齐全烂尾了怎么办 5万罚款交不起怎么办 炼铅环保手续要怎么办 贴膏药过敏红肿太痒了怎么办 没工作想贷款5万怎么办 燃气管超过2米了怎么办 建行燃气卡丢了怎么办 周浦燃气卡丢了怎么办 长沙燃气卡丢了怎么办 郑州燃气卡丢了怎么办 租房燃气卡丢了怎么办 洛阳燃气卡丢了怎么办 零线火线都带电怎么办 档案里年龄错了怎么办 档案年龄大了怎么办呢 吃菌子致幻了怎么办 野外吃了毒蘑菇怎么办 头顶头发稀少怎么办头顶头发稀 遇见无赖的人该怎么办 扶了老人被讹诈怎么办 遇见碰瓷讹人的怎么办 假机油用了4年怎么办 苹果6手机变砖头怎么办 苹果8升级变砖头怎么办 苹果id锁变砖头怎么办 钥匙断在锁里了怎么办? u型锁忽然打不开怎么办 密码门锁没电了怎么办 智能门锁没电了怎么办 十字锁钥匙丢了怎么办 罐头的拉环断了怎么办 锁坏了 打不开了怎么办 门锁锁不起来了怎么办 卧室门锁舌断了怎么办