ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android自定义控件

2022-01-27 14:35:12  阅读:149  来源: 互联网

标签:控件 自定义 public attrs context import Android android view


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

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有