要通过xml创建一个计算器按键视图,可以按照以下步骤进行操作:

  1. 首先,在res/layout目录下创建一个新的xml文件,例如calculator_keypad.xml

  2. calculator_keypad.xml中,使用LinearLayout或者GridLayout来布局计算器按键。例如:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- 第一行 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

    </LinearLayout>

    <!-- 第二行 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />

    </LinearLayout>

    <!-- 其他行 -->

</LinearLayout>
  1. 在其他布局文件中,可以使用<include>标签来引用calculator_keypad.xml,以便在其他页面中重复使用计算器按键视图。例如:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 其他页面内容 -->

    <include
        layout="@layout/calculator_keypad" />

    <!-- 其他页面内容 -->

</LinearLayout>

这样,calculator_keypad.xml中的计算器按键视图就可以在其他页面中重复使用并显示出来了

通过xml创建一个计算器按键视图然后这个视图可以重复使用并显示在其他页面Android

原文地址: https://www.cveoy.top/t/topic/hE3G 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录