Android的自定義控件(繼承佈局)

Android自定義控件:
找不到本身想要的控件?不如本身動手作一個!
(注:文章使用SM.MS圖牀,國慶期間可能訪問較慢)
沿用上一次的知識,背景不止能夠用在按鈕中,也能夠應用在佈局中,可是這一次只修改背景。
爲佈局編寫不一樣狀態背景bg_login.xml:android

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="false" android:drawable="@drawable/bg_login_default"/>

    <item android:state_pressed="true" android:drawable="@drawable/bg_login_pressed"/>

</selector>

其中bg_login_default.xml:app

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#FFFFFF"/>

</shape>

另外一個相似,不介紹。
接下來編寫咱們想要的佈局
item_layout.xml:ide

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical"
        android:background="@drawable/bg_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="40dp">

            <TextView
                android:id="@+id/tv_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:gravity="center"
                android:text="標題"
                android:textColor="#454444"
                android:textSize="15sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tv_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dp"
                android:layout_marginRight="10dp"
                android:gravity="center"
                android:text="內容"
                android:textColor="#646464"
                android:textSize="14sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/arrow"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/arrow"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:scaleType="center"
                android:src="@mipmap/arrow"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#898888"/>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

加了兩個Text和一個Image,還有一個灰色的背景條用於分割兩個組件。注:圖片的背景是在阿里的圖標庫裏找的
效果:佈局

還行,湊合着看......
而後編寫自定義控件的類ItemView:this

package com.fitsoft;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemView extends ConstraintLayout {

    TextView tv_left;
    TextView tv_right;
    ImageView tv_arrow;

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);

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

        tv_left = findViewById(R.id.tv_left);
        tv_right = findViewById(R.id.tv_right);
        tv_arrow = findViewById(R.id.arrow);

    }

    /**
     * 設置內容
     * @param tvLeftStr 標題文字
     * @param tvRightStr 內容文字
     * @param isShowImage 是否顯示圖片
     */
    public void setView(String tvLeftStr, String tvRightStr, Boolean isShowImage){

        if(tvLeftStr != null){
            tv_left.setText(tvLeftStr);
        }

        if(tvRightStr != null){
            tv_right.setText(tvRightStr);
        }

        if(isShowImage){
            tv_arrow.setVisibility(View.VISIBLE);
        }else {
            tv_arrow.setVisibility(View.GONE);
        }

    }
}

很簡單的一些方法,首先綁定佈局,而後將組件的設置內容的方法寫在了一個方法中。
而後在咱們須要加載自定義控件的地方加上,我加在了主佈局activity_main.xml中:3d

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:background="#50E7B2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.fitsoft.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.fitsoft.ItemView
            android:id="@+id/item1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"/>

        <com.fitsoft.ItemView
            android:id="@+id/item2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"/>

        <com.fitsoft.ItemView
            android:id="@+id/item3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"/>

        <com.fitsoft.ItemView
            android:id="@+id/item4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"/>

    </LinearLayout>
</android.support.constraint.ConstraintLayout>

加了四個剛剛寫完的自定義控件
在類中設置相應屬性:code

package com.fitsoft;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ItemView itemView1 = findViewById(R.id.item1);
        ItemView itemView2 = findViewById(R.id.item2);
        ItemView itemView3 = findViewById(R.id.item3);
        ItemView itemView4 = findViewById(R.id.item4);

        itemView1.setView("姓名", "張", true);
        itemView2.setView("年齡", "", true);
        itemView3.setView("", "瀋陽", false);
        itemView4.setView("出生地", "", true);

    }
}

爲了效果嗎明顯,我把背景改了,另外還要注意,佈局若是使用不一樣狀態,須要設置clickable屬性爲true,同時AndroidStudio會提示你設置focusable屬性,將其設置爲true就OK
效果圖:xml

相關文章
相關標籤/搜索