我创建了一个 ConstrantLayout 类
public class AboutView extends ConstraintLayout {
TextView about_txt;
TextView dr_txt;
public AboutView(Context context) {
super( context );
init();
}
public AboutView(Context context, AttributeSet attrs, int defStyleAttr) {
super( context, attrs, defStyleAttr );
init();
}
private void init() {
LayoutInflater inflater = LayoutInflater.from( getContext() );
inflater.inflate( R.layout.about_layout,this );
about_txt = (TextView) findViewById(R.id.about_txt);
dr_txt = (TextView) findViewById(R.id.dr_txt);
}
}
布局 about_layout.XML 文件以膨胀到类中
最佳答案
按照您希望自定义 View 的外观创建布局。没有什么复杂的。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns: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="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/about_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
tools:text="About" />
<TextView
android:id="@+id/dr_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Text" />
</android.support.constraint.ConstraintLayout>
这样您就可以设置约束并使用编辑器编辑布局。
完成编辑后,我建议将 Root View ConstraintLayout 更改为merge。
通过合并,您将不会在自定义 View 中有额外的布局。请注意 - merge 属性会被忽略。
关于java - 如何在 ConstraintLayout 类中使用 android 扩展 XML 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45422201/