【问题标题】:Android Button Panel安卓按键面板
【发布时间】:2014-06-23 12:28:56
【问题描述】:

我正在创建一个 android 应用程序,并且需要在屏幕底部有一个导航面板,用户可以使用它来浏览应用程序,并跳过他们不想执行的任何步骤,例如他们可以只是选择第 5 步和第 10 步,其余的省略。这将是一个面板,有 7 个按钮,向下 3 个按钮,提供 21 个按钮。 XML代码如下;

<LinearLayout
            android:layout_width="match_parent"            
            android:layout_height="wrap_content"            
            android:orientation="vertical" 
            android:layout_alignParentBottom="true"           
            android:weightSum="3">

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"  
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button2"
            android:layout_below="@+id/button2"
            android:weightSum="7">
  <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="01" />

    <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="02" />
      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="03" />
        <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="04" />
          <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="05" />
            <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="06" />
                <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="07" />

      </LinearLayout>   

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"  
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button2"
            android:layout_below="@+id/button2"
            android:weightSum="7">
  <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="08" />

    <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="09" />
      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="10" />
        <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="11" />
          <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="12" />
            <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="13" />
                <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="14" />

      </LinearLayout>   

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"  
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button2"
            android:layout_below="@+id/button2"
            android:weightSum="7">
  <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="15" />

    <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="16" />
      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="17" />
        <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="18" />
          <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="19" />
            <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="20" />
                <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:onClick="Previous"
          android:text="21" />

      </LinearLayout>   

   </LinearLayout>   

大约有 50 个类(或此应用中的不同屏幕),并且每个类都需要存在此按钮面板。每个按钮还必须有一个 onClickListener 来告诉它该做什么。

我的问题涉及两点,第一点与 XML 文件有关。

由于这个“按钮面板”将出现在每个屏幕上,有没有一种方法可以创建一个主 XML 文件,可用于在每个屏幕上创建此面板,而不必一次重写 50 次相同的代码app-似乎效率很高。

第二点与 onClickListener 有关。同样,是否可以创建一个主侦听器,每次出现时都将应用于此按钮面板。我将创建一个处理事件的主侦听器类,但如果没有适当的主类,每个单独的类都必须为每个类中的每个单独按钮设置侦听器——这再次看起来效率非常低,因为会有大量重复50 个类中的相同代码。

我曾考虑过使用冰淇淋三明治,但这似乎不太符合我的需求。

【问题讨论】:

    标签: android button


    【解决方案1】:

    创建可重复使用的布局

    创建单独的导航面板布局。

    假设这是您的导航面板布局 (panelbar.xml)

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width=”match_parent”
    android:layout_height="wrap_content"
    android:background="@color/footerbar_bg">
    
    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:src="@drawable/gafricalogo" />
    

    只需将其包含在您的所有 XML 中

    现在像这样在每个 XML 中包含您的导航面板。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">
    
    <include layout="@layout/panelbar"/>
    
    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />
    
    ...
    

    来源:For more detail info

    【讨论】:

      【解决方案2】:

      您可以单独创建一个带有按钮面板的xml文件,并在其他xml中使用include标签,如下所示:

      <include
          android:id="@+id/panel"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          layout="@layout/button_panel" />
      

      onClickListener而言,您可以查看this的帖子是否对您有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 2011-05-25
        相关资源
        最近更新 更多