gpt4 book ai didi

android - 每个 xml 中的按钮。避免在每个 xml 中编写代码

转载 作者:行者123 更新时间:2023-11-29 16:02:39 27 4
gpt4 key购买 nike

我有 5 个 xml。在我创建 10 个按钮的每个 xml 中。我的问题是无论如何要避免在每个 xml 中为按钮编写相同的代码。在每个 xml 中也存在不同的元素。只有 10 个按钮是在每个 xml 中都很常见。例如:

xml1-> button 1 button2   button3 button4(present at top) textbox
xml2-> button 1 button2 button3 button4(present at top) editbox
xml3-> button 1 button2 button3 button4(present at top) imagebutton
xml4-> button 1 button2 button3 button4(present at top) listview

这不是我的原始代码

   <RelativeLayout 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"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</relativelayout>

最佳答案

你可以在一个 xml 中有一个按钮,然后在你想要的布局中使用 include 标签。

<include android:id="@+id/b1" layout="@layout/button_layout" />

这是 Romain Guy 的博客

http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/

关于android - 每个 xml 中的按钮。避免在每个 xml 中编写代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22641752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com