其他分享
首页 > 其他分享> > Android自定义控件

Android自定义控件

作者:互联网

自定义布局加一个条栏
在这里插入图片描述
代码如下

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

    <View
        android:id="@+id/view_test"
        android:background="#F00"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_marginTop="100dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</FrameLayout>

自定义对应的Java文件
在这里插入图片描述

package com.meetdilse.store.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.meetdilse.store.R;

public class TestLayout extends FrameLayout {

    private final FrameLayout mFrameLayout;

    public TestLayout(@NonNull Context context) {
        this(context,null);
    }

    public TestLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TestLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public TestLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_framelayout, this, true);

        mFrameLayout = view.findViewById(R.id.fl_container);

    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (mFrameLayout != null && child.getId() != R.id.view_test) {
            mFrameLayout.addView(child, index, params);
        } else {
            super.addView(child, index, params);
        }
    }
}

应用自定义控件如下
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <!--创建TextView-->
    <TextView
        android:text="pager_4"
        android:textSize="28sp"
        android:textColor="#00f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.meetdilse.store.view.TestLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View
                android:background="#FF0"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="100dp" />
            <View
                android:background="#FF0"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_height="100dp" />
            <View
                android:background="#FF0"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="100dp" />
        </LinearLayout>

    </com.meetdilse.store.view.TestLayout>

</LinearLayout>

标签:控件,自定义,public,attrs,context,import,Android,android,view
来源: https://blog.csdn.net/weixin_44911775/article/details/122717101