java - 如何以编程方式将此 XML 代码写入 CustomView 构造函数?

标签 java android android-studio

**Guys i need help i am trying learn to **do** front-end work programmatically? How i should to do this XML layout in Custom View constructor? Thanks in advance! How i should make custom view constructor like this XML?**

    `<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     app:cardCornerRadius="4dp"
     android:layout_margin="2dp">

<RelativeLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="4dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
       />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imageView"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text2"
        android:textSize="14sp"
        android:textStyle="italic"
        android:textColor="@android:color/black"
        android:layout_below="@id/textView1"
        android:layout_toEndOf="@+id/imageView"/>


</RelativeLayout>

`

伙计们,我需要帮助我正在尝试学习以编程方式进行前端工作吗?我应该如何在自定义 View 构造函数中执行此 XML 布局?提前致谢!我应该如何制作像这样的 XML 的自定义 View 构造函数?

最佳答案

可以通过编程方式构建 View ,但您确实应该使用 XML。通过这种方式,您的应用程序行为、逻辑(在代码中)和它的 UI(在 XML 中)是分离的。此外,在 XML 中创建布局要快得多。

这是一个使用自定义 View 的例子,里面有一个 TextView :

public class MyRelativeLayout extends RelativeLayout {

    private TextView textView;

    public MyRelativeLayout(Context context) {
         super(context); 
         textView = new TextView(context);
         textView.setText("Hello");

         //we must use LayoutParams objects to manage layout properties programmatically
          RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
                    (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          params.leftMargin = 5;
          params.rightMargin = 5;
          textView.setLayoutParams(params);

          addView(textView); //add to our layout
    }

} 

我只添加了一个 TextView ,除了侧边距什么都没有设置,但是代码已经很长了,同时你可以在 XML 中完成这一切,就像这样简单:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:text="Hello"/>
</RelativeLayout>

布局越复杂,用 XML 创建它就越好。长话短说,使用 XML 进行布局,并在您的代码中扩充它,而不是创建自定义 View 。

关于java - 如何以编程方式将此 XML 代码写入 CustomView 构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58380429/

相关文章:

android - [Unity][Firebase] Dex : Error converting bytecode to dex: Error Building Player

java - 如何在android中获取文件数组的大小?

android-studio - 在 Android Studio 中调试 gradle 文件

android - 无法使用 Android Studio 和 ndkBuild 在 C++ 中到达断点

android - phonegap/jquerymobile 应用程序的 Android 模拟器上未显示图像

Java 长轮询 : Separate Thread?

java - 无法启动Camel 1.5.0

java - 如何使用正则表达式获取此 URL 参数

java - Android/Java 渲染问题 - 无法解析资源/无法转换为可绘制对象

java - 从 firebase Firestore 数据库中的存储下载图像时出错