《第一行代码》学习笔记四:布局和自定义控件

一:LinearLayout,线性布局,把它所包含的控件在线性方向上依次排列。orientation有两个值,默认是horizontal,水平排列,还有一个是vertical,垂直排列。注意:,如果值为horizontal,内部控件就不能将宽度设为match_parent,不然单独一个控件就占满屏幕,其他控件显示不出来,同理,vertical的高度也不能设为match_parent。

android:gravity是指定文字在控件中的对齐方式,android:layout_gravity是指控件在布局中的对齐方式

android:layout_weight,这个属性是让我们用比例的方式来指定控件的大小,使用该属性的时候,android:layout_width应赋值为0

二:RativeLayout,相对布局,通过定位让控件出现在任何位置。

android:layout_alignParentLeft在父布局的左边。。。

android:layout_above一个控件位于另一个控件的上方。。。

android:layout_toLeftOf一个控件位于另一个控件的左侧。。。

三:FrameLayout,所有控件都会摆放在布局的左上角。

四:TableLayout用表格的方式来排列控件

android:layout_span="2"让控件占据两列的空间

android:stretchColumns=“1”如果表格不能占满整个屏幕宽度,就将第二列进行拉伸,0代表第一列,2代表第三列




自定义控件

一:引入布局

新建一个布局,然后在要引用的布局文件中加<include layout="@layout/title"/>

二:自定义控件

要是引入的布局中有控件是能响应事件的,那每次引用这个布局文件就要注册一次,就很麻烦

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal"
    >
    
    
    
    <Button 
        android:id="@+id/title_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_margin="5dip"
        android:background="@drawable/back"
        android:text="Back"
        android:textColor="#fff"
        />
    
    
    <TextView 
        android:id="@+id/title_text"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_weight="1"
        android:layout_marginTop="10dip"
        android:gravity="center"
        android:text="Title Test"
        android:textColor="#000"
        android:textSize="24sp"
        />
    
    
    
    <Button
        android:id="@+id/title_edit"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="top" 
        android:layout_margin="5dip"
        android:background="@drawable/meun"
        android:text="Edit"
        android:textColor="#fff"/>
        
    

</LinearLayout>

这是布局,让这个布局和按钮成为一个自定义控件

package com.example.uicustomviews;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class TitleLayout extends LinearLayout implements OnClickListener {
	public TitleLayout(Context context,AttributeSet attrs){
		super(context,attrs);
		LayoutInflater.from(context).inflate(R.layout.title, this);
		Button titleBack=(Button)findViewById(R.id.title_back);
		Button titleEdit=(Button)findViewById(R.id.title_edit);
		titleBack.setOnClickListener(this);
		titleEdit.setOnClickListener(this);
		
	}
	
	public void onClick(View v){
		switch (v.getId()){
		case R.id.title_back:
			((Activity) getContext()).finish();
		
		break;
		case R.id.title_edit:
			Toast.makeText(getContext(), "you clicked edit button", Toast.LENGTH_SHORT).show();
			break;
			default:
				break;
			
		}
	}

}


新建一个类继承LinearLayout,重写构造函数,类中把按钮等控件注册一遍,以后引用这个控件就无须再注册了。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >
    
    <com.example.uicustomviews.TitleLayout
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        ></com.example.uicustomviews.TitleLayout>

   

</LinearLayout>

这是引用。


猜你喜欢

转载自blog.csdn.net/qq_38167925/article/details/80038005
今日推荐