繁体   English   中英

如何在ConstraintLayout类中为android膨胀XML Layout?

[英]How to android inflate XML Layout in ConstraintLayout class?

我创建一个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文件以膨胀为类

创建所需的自定义视图外观布局。 没有什么复杂的。

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

这样,您可以设置约束并使用编辑器编辑布局。

完成编辑后,建议您将根视图ConstraintLayout更改为merge

通过合并,您将在自定义视图中没有额外的布局。 注意- 合并属性将被忽略。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM