如何简单地创建自定义视图而不重复线性布局?



我发现创建自定义视图的最简单方法是让它继承自 LinearLayout,这样我就不必处理诸如覆盖 onLayout(( 方法之类的烦人事情。我在膨胀的关联 XML 文件的根目录中还有一个 LinearLayout,因此根目录中有 2 个。

如何通过删除这个额外的线性布局之一来优化这一点,但保持创建自定义视图的简单性?

MyToolbar.kt:

class MyToolbar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) :
LinearLayoutCompat(context, attrs, defStyleAttr) {
private val binding = MyToolbarBinding.inflate(LayoutInflater.from(context), this, true)
init {
// [...] Initialization of my view ...
}
}

my_toolbar.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<!-- Actual content of my view -->
</LinearLayout>

经过@Pawel和@Cata建议,我现在有什么。这不起作用,LinearLayout使用父级的整个高度,但它应该只包装其内容。

MyToolbar.kt:

class MyToolbar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) :
LinearLayoutCompat(context, attrs, defStyleAttr) {
private val binding
init {
// Tried to add the attributes here as they seems ignored on the `merge` tag
gravity = Gravity.CENTER_VERTICAL
orientation = HORIZONTAL
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
binding = MyToolbarBinding.inflate(LayoutInflater.from(context), this)
// [...] Initialization of the view ...
}
}

my_toolbar.xml:

<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<!-- Actual content of the view -->
</merge>

正如 Pawel 建议的那样,您可以使用 merge 来做到这一点。

这是来自开发人员网站的示例:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Here you can add your custom content views.. -->
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</merge>

最新更新