Android开发:使用XML创建可复用计算器按键视图
首先,在res/layout文件夹中创建一个名为"calculator_button_view.xml"的XML文件,添加以下代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id\/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id\/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id\/button_1"
android:text="2" />
<Button
android:id="@+id\/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id\/button_2"
android:text="3" />
<!-- 添加其他按键按钮 -->
</RelativeLayout>
然后,在其他页面的布局文件中,通过include标签引用这个视图,并在下方显示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 其他页面内容 -->
<include
android:id="@+id\/calculator_buttons"
layout="@layout\/calculator_button_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
这样就可以在其他页面中重复使用这个计算器按键视图,且显示在页面的下方。
原文地址: https://www.cveoy.top/t/topic/pn8T 著作权归作者所有。请勿转载和采集!