我正在try 创建一个自定义视图,以替换我在多个位置使用的特定布局,但我很难做到这一点.

基本上,我想要替换这个:

<RelativeLayout
 android:id="@+id/dolphinLine"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
    android:layout_centerInParent="true"
 android:background="@drawable/background_box_light_blue"
 android:padding="10dip"
 android:layout_margin="10dip">
  <TextView
   android:id="@+id/dolphinTitle"
   android:layout_width="200dip"
   android:layout_height="100dip"
   android:layout_alignParentLeft="true"
   android:layout_marginLeft="10dip"
   android:text="@string/my_title"
   android:textSize="30dip"
   android:textStyle="bold"
   android:textColor="#2E4C71"
   android:gravity="center"/>
  <Button
   android:id="@+id/dolphinMinusButton"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_toRightOf="@+id/dolphinTitle"
   android:layout_marginLeft="30dip"
   android:text="@string/minus_button"
   android:textSize="70dip"
   android:textStyle="bold"
   android:gravity="center"
   android:layout_marginTop="1dip"
   android:background="@drawable/button_blue_square_selector"
   android:textColor="#FFFFFF"
   android:onClick="onClick"/>
  <TextView
   android:id="@+id/dolphinValue"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_marginLeft="15dip"
   android:background="@android:drawable/editbox_background"
   android:layout_toRightOf="@+id/dolphinMinusButton"
   android:text="0"
   android:textColor="#2E4C71"
   android:textSize="50dip"
   android:gravity="center"
   android:textStyle="bold"
   android:inputType="none"/>
  <Button
   android:id="@+id/dolphinPlusButton"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_toRightOf="@+id/dolphinValue"
   android:layout_marginLeft="15dip"
   android:text="@string/plus_button"
   android:textSize="70dip"
   android:textStyle="bold"
   android:gravity="center"
   android:layout_marginTop="1dip"
   android:background="@drawable/button_blue_square_selector"
   android:textColor="#FFFFFF"
   android:onClick="onClick"/>
</RelativeLayout>

据此:

<view class="com.example.MyQuantityBox"
    android:id="@+id/dolphinBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:myCustomAttribute="@string/my_title"/>

所以,我不想要自定义布局,我想要一个自定义视图(这个视图不可能有子视图).

从MyQuantityBox的一个实例到另一个实例,唯一可以更改的是标题.我非常希望能够在XML中指定这一点(就像我在最后一行XML中所做的那样)

我怎么才能做到这一点呢?我是否应该将RelativeLayout放在/res/layout中的XML文件中,并将其扩展到MyBoxQuantity类中?如果是,我该怎么做?

谢谢

推荐答案

是的,你能做到.RelativeLayout、LinearLayout等都是视图,因此自定义布局就是自定义视图.只是想考虑一下,因为如果你想创建一个自定义布局,你可以.

您要做的是创建一个复合控件.您将创建RelativeLayout的子类,在代码(TextView等)中添加所有组件,并在构造函数中读取从XML传入的属性.然后可以将该属性传递给标题文本视图.

http://developer.android.com/guide/topics/ui/custom-components.html

Android相关问答推荐

如何将绘图中的矩形与roboflow和喷气背包组合对齐?

更新画布上的绘图以具有水平填充

如何将Hilt添加到Android Studio中的Kotlin项目中?

RemoteActivityHelper.startRemoteActivity不适用于Android Wear OS 4模拟器

Jetpack编写使用自定义主题覆盖库中主题部分

如何在使用带有底部导航组件的片段管理器时更改片段工具栏的标签

在 Bash 脚本中使用 XMLLINT 解析 XML 单元测试文件,并将其放入数组中以表示成功和失败

从我的 Android 应用程序发送到 Gin 时失败,但从 Postman 发送到 Gin 时成功

当 Jetpack Compose 中的第一个文本很长时,将省略号添加到第一个文本

需要在按钮 onclick 上从 string.xml 获取值. @Composable 调用只能在@Composable 函数的上下文中发生

在 kotlin 中动态添加 GridView

React Native Android 应用程序在调试模式下运行良好,但当我们发布 apk 时,它会生成旧版本的应用程序

Jetpack Compose 部分或开放侧边框

每次在 Jetpack Compose 中调用导航

如何在 Jetpack Compose 中添加多个标签

Android Compose:LazyColumn 和 Column with verticalScroll 的区别

在alert 对话框生成器中启动协程

使用 Room 在 SQLite 中保存复杂的 JSON 响应

如何使用 recyclerview 实现这样的布局?

如何在 Jetpack Compose 中填充矢量图像的背景?