Android中引入佈局和和自定義控制元件
阿新 • • 發佈:2018-11-02
首先是引入佈局:
1.我們自己新建一個layout,就是一個標題欄。
2.然後在我們的mainactivity_layout中使用一個語句就可以實現。
<?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" > <Button android:id="@+id/title_back" android:layout_margin="5dp" android:text="back" android:textColor="#000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/title_text" android:layout_margin="5dp" android:text=" title text" android:textColor="#000" android:layout_width="0dp" android:layout_weight="1" android:textSize="24sp" android:layout_height="wrap_content" /> <Button android:id="@+id/title_edit" android:layout_margin="5dp" android:text="edit" android:textColor="#000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
<incluede layout="@layout/title"/>
這樣就可以實現使用我們自己設計的介面了。
還有一點需要注意,我們要把系統中自帶的標題隱藏掉。
package com.example.sunshunli.uicustomviews; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ActionBar actionBar = getSupportActionBar(); if (actionBar !=null){ actionBar.hide(); } } }
自定義控制元件:
引入佈局,解決了重複編寫佈局程式碼的問題,但是如果我們的佈局中要求能夠響應事件,那麼我們還是要在每個活動中編寫我們的註冊程式碼,向我們這個,back 的按鈕,不論在哪個活動裡都是一個作用銷燬當前的活動。如果在每一個活動中都寫,那麼也會很麻煩。索引我們可以使用自定義控制元件。
package com.example.sunshunli.uicustomviews; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ActionBar actionBar = getSupportActionBar(); if (actionBar !=null){ actionBar.hide(); } } }
我們的mainactivity.java沒有變化。
package com.example.sunshunli.uicustomviews;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TitleLayout extends LinearLayout {
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(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();
}
});
}
}
新建一個類繼承LinearLayout,讓他成為我們的自定義標題欄控制元件。
首先我們重寫LinearLayout中帶有兩個引數的建構函式,在佈局中引入TitleLayout控制元件就會呼叫這個建構函式。然後在建構函式中需要對標題欄佈局進行動態載入。
需要藉助
LayoutInflater來實現,通LayoutInflater的from方法可以構建一個LayoutInflate的物件,然後呼叫inflate(R.layout.title, this)方法就可以實現動態載入佈局,需要兩個引數這個方法,第一個是我們要載入的layout,第二個是給載入好的佈局檔案新增一個父佈局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.sunshunli.uicustomviews.TitleLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
最後這個是我們在activitymain_layout中新增我們的自定義控制元件的方法。