"First Line of Code" Study Notes 4: Layout and Custom Controls

One: LinearLayout, linear layout, arranges the controls it contains in order in a linear direction. The orientation has two values, the default is horizontal, which is arranged horizontally, and the other is vertical, which is arranged vertically. Note: If the value is horizontal, the internal control cannot set the width to match_parent, otherwise a single control will fill the screen and other controls will not be displayed. Similarly, the vertical height cannot be set to match_parent.

android:gravity is the alignment of the specified text in the control, android:layout_gravity is the alignment of the control in the layout

android:layout_weight, this attribute allows us to specify the size of the control in a proportional way. When using this attribute, android:layout_width should be assigned a value of 0

Two: RativeLayout, relative layout, allows controls to appear in any position through positioning.

android:layout_alignParentLeft is on the left side of the parent layout. . .

android:layout_above A control is above another control. . .

android:layout_toLeftOf A control is positioned to the left of another control. . .

Three: FrameLayout, all controls will be placed in the upper left corner of the layout.

Four: TableLayout uses tables to arrange controls

android:layout_span="2" makes the control occupy two columns of space

android:stretchColumns="1" If the table cannot occupy the entire width of the screen, the second column will be stretched, 0 means the first column, 2 means the third column




custom control

One: Introducing the layout

Create a new layout and add <include layout="@layout/title"/> to the layout file to be referenced

Two: custom controls

If there are controls in the imported layout that can respond to events, it is very troublesome to register each time the layout file is referenced.

<?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>

Here is the layout, make this layout and the button a custom control

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;
			
		}
	}

}


Create a new class that inherits LinearLayout, rewrite the constructor, and register controls such as buttons in the class. You do not need to register this control in the future.

<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>

Here is the quote.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324694696&siteId=291194637