【问题标题】:creating custom xml view创建自定义 xml 视图
【发布时间】:2022-08-02 18:11:25
【问题描述】:

我是 android 开发的新手,听说过 custom library(module),我看到了一些关于如何创建模块和创建类的示例。但我不知道如何创建自定义 xml 视图并对其进行设计,例如,

<com.example.customButton
android:layoutwidth=\"120dp\"
android:layoutheight=\"50dp\">
</com.example.customButton>

谁能帮我 ?。

    标签: java android module


    【解决方案1】:

    在 android 中要创建自定义视图,您需要创建一个扩展 View 类的类,例如:

    class PieChart extends View {
        public PieChart(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    }
    

    检查Android documentation

    但我认为您的目标是为按钮创建style,为此创建自定义样式以应用于您的按钮check this

    希望这可以帮助!

    【讨论】:

      【解决方案2】:

      我可能会建议您使用诸如材质按钮之类的原生组件来设计按钮,而不是使用自定义库,因为它们将来可能会被弃用并且您不想依赖它。 您始终可以通过设置特定属性来自定义按钮、文本视图、编辑文本。

      让我给你举个例子:

      <com.google.android.material.button.MaterialButton
                      android:id="@+id/signup_button"
                      android:layout_width="wrap_content"
                      android:layout_height="50dp"
                      android:layout_marginTop="50dp"
                      android:backgroundTint="@color/purple_200"
                      android:text="Sign up"
                      android:textAlignment="center"
                      android:textColor="@color/white"
                      app:cornerRadius="4dp"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-24
        • 1970-01-01
        • 2012-07-25
        • 2015-06-12
        • 2016-04-30
        相关资源
        最近更新 更多