java - 如何在 ConstraintLayout 类中使用 android 扩展 XML 布局?

标签 java android layout-inflater android-constraintlayout

我创建了一个 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/

相关文章:

java - 注册期间用于密码检查的简单正则表达式是什么?

android - android 中对话框中 ListView 的高度未根据 ListView 中的项目数设置;当选项数量较少时会出现空白

android - findViewById 更改 for 循环中的所有出现

android - 从后台 View 创建位图

java - 调整框架大小时,JLabel 的位置更改为奇怪的位置

Java接口(interface)实现工厂对象

java - 在java中使用多个连接下载单个文件?

android - Android RatingBar 对某些设备有 "bad"阈值,修复了吗?

Android 重启应用程序

android - ArrayAdapter 中的错误 - LayoutInflater 抛出错误