android 自定義view

android怎麼自定義view呢?不知道的小夥伴來看看小編今天的分享吧!

android可以通過組合控制元件來實現自定義view。組合控制元件就是將系統原有的控制元件進行組合,構成一個新的控制元件。這種方式下,不需要開發者自己去繪製圖上顯示的內容,也不需要開發者重寫onMeasure,onLayout,onDraw方法來實現測量、佈局以及draw流程。

具體操作:

1、定義標題欄佈局檔案

 定義標題欄的佈局檔案custom_title_view.xml,將返回按鈕和標題文字進行組合。這一步用於確定標題欄的樣子,程式碼如下所示:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@android:color/holo_orange_light">

<Button

android:id="@+id/btn_left"

 android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_marginLeft="5dp"

android:text="Back"

android:textColor="@android:color/white" />

<TextView

android:id="@+id/title_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="Title"

android:textColor="@android:color/white"

android:textSize="20sp" />

</RelativeLayout>

2、根據給定佈局實現自定義View

public class CustomTitleView extends FrameLayout implements View.OnClickListener {

private View.OnClickListener mLeftOnClickListener;

private Button mBackBtn;

private TextView mTittleView;

public CustomTitleView(@NonNull Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

LayoutInflater.from(context).inflate(R.layout.custom_title_view, this);

mBackBtn = findViewById(R.id.btn_left);

mBackBtn.setOnClickListener(this);

mTittleView = findViewById(R.id.title_tv);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btn_left:

if (mLeftOnClickListener != null) {

mLeftOnClickListener.onClick(v);

 }

break;

}

}

public void setLeftOnClickListener(View.OnClickListener leftOnClickListener) {

mLeftOnClickListener = leftOnClickListener;

}

public void setTittle(String title){

mTittleView.setText(title);

}

}

說明:

(1)程式碼中對外提供了兩個介面,一是動態設定標題,二是使用者可以自定義返回按鈕的點選事件。

(2)CustomTitleView的建構函式,要選擇兩個引數的,選擇其它引數的建構函式會報錯。這一點是筆者開發機測試的結果,暫時不清楚是不是所有手機上都是這樣。

(3)這裡是繼承的FrameLayout,但是繼承LinearLayout,RelativeLayout等系統佈局控制元件都可以。之所以要繼承這些系統現成的ViewGroup,是因為這樣可以不用再重寫onMeasure,onLayout等,這樣省事很多。由於這裡是一個佈局控制元件,要用LayoutInflater來填充,所以需要繼承ViewGroup,如果繼承View的直接子類,編譯會不通過。所以,CustomTitleView自己就是一個容器,完全可以當成容器使用,此時CustomTitleView自身的內容會和其作為父佈局新增的子控制元件,效果會疊加,具體的疊加效果是根據繼承的容器特性決定的。

3、在Activity的佈局檔案中新增CustomTitleView。

在Activity的佈局檔案activity_custom_view_compose_demo.xml中,像使用系統控制元件一樣使用CustomTitleView即可。CustomTitleView自己就是繼承的現成的系統佈局,所以它們擁有的屬性特性,CustomTitleView一樣擁有。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<com.example.demos.customviewdemo.CustomTitleView

android:id="@+id/customview_title"

android:layout_width="match_parent"

android:layout_height="wrap_content">

</com.example.demos.customviewdemo.CustomTitleView>

</RelativeLayout>

 4、在Activity中操作CustomTitleView,程式碼如下:

 1 public class CustomViewComposeDemoActivity extends AppCompatActivity { 2  3     private CustomTitleView mCustomTitleView; 4     @Override 5     protected void onCreate(Bundle savedInstanceState) { 6         super.onCreate(savedInstanceState); 7         setContentView(R.layout.activity_custom_view_compose_demo); 8         mCustomTitleView = findViewById(R.id.customview_title); 9         mCustomTitleView.setTittle("This is Title");10         mCustomTitleView.setLeftOnClickListener(new View.OnClickListener() {11             @Override12             public void onClick(View v) {13                 finish();14             }15         });16 17     }18 }

在第8行中,獲取到CustomTitleView例項,第9行設定標題文字,第10行自定義“Back”按鈕點選事件。

5、效果圖

按照如上的4步,就通過組合控制元件完成了一個比較簡單的自定義標題欄。

以上就是小編今天的分享了,希望可以幫助到大家。