<?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>
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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。