【问题标题】:How to create android button bar?如何创建android按钮栏?
【发布时间】:2017-03-31 08:56:14
【问题描述】:

我需要创建一个水平的按钮列表,没有像这样的分隔空间
每个按钮都将包含一个文本和一个我知道的关于 buttonbarlayout 的图标,但它对我不起作用,请有人帮助我,提前谢谢

【问题讨论】:

标签: android android-layout android-button


【解决方案1】:
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableTop="@drawable/read"
        android:text="Rewards" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableTop="@drawable/read"
        android:text="Places" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableTop="@drawable/read"
        android:text="Challenges" />
</LinearLayout>

使用具有水平方向的 LinearLayout 和 layout_weight=1 的三个按钮

【讨论】:

    【解决方案2】:

    这个有 BottomNavigationView 元素

    在你的布局中:

     <android.support.design.widget.BottomNavigationView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/navigation"
     android:layout_width="match_parent"
     android:layout_height="56dp"
     android:layout_gravity="start"
     app:menu="@menu/my_navigation_items" />
    

    res/menu/my_navigation_items.xml:

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/action_search"
              android:title="@string/menu_search"
              android:icon="@drawable/ic_search" />
        <item android:id="@+id/action_settings"
              android:title="@string/menu_settings"
              android:icon="@drawable/ic_add" />
        <item android:id="@+id/action_navigation"
              android:title="@string/menu_navigation"
              android:icon="@drawable/ic_action_navigation_menu" />
    </menu>
    

    【讨论】:

      【解决方案3】:
       <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="50dp"
              android:weightSum="4"
              android:id="@+id/func"
              android:layout_above="@+id/btn_next">
      
              <Button
                  android:id="@+id/btn_bgm"
                  android:layout_width="wrap_content"
                  android:layout_weight="1"
                  android:background="#3279CE"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/pager_image"
                  android:text="BitRate"/>
      
              <Button
                  android:id="@+id/btn_time"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:background="#3279CE"
                  android:text="time"
                  />
      
      
              <Button
                  android:id="@+id/btn_effect"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:background="#3279CE"
                  android:text="effect" />
              <Button
                  android:id="@+id/frame"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:background="#3279CE"
                  android:text="FRAME" />
      
      
          </LinearLayout>
      

      【讨论】:

      • 非常感谢您的回答,这和我想做的很相似,我只是有一个问题:我希望图标和文字彼此相邻并位于中心按钮的 |------ 文字+图标 ------ |
      • @Caroline,没问题,只需使用android:drawableRight="@drawable/..."在文本右侧放置一个drawable。
      【解决方案4】:
      private LinearLayout tabLayout; 
      
         FrameLayout tab1 = (FrameLayout) LayoutInflater.from(this).inflate(R.layout.tab_cell, null);
          tab1.findViewById(R.id.click_view).setOnClickListener(this);
          tab1.findViewById(R.id.click_view).setTag(First_TAB);
          textviewone = (TextView) tab1.findViewById(R.id.tab_header);
          textviewone.setWidth(SMALL_TAB_WIDTH);
          textviewone.setText("Rewards");
          textviewone.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.picture_s, 0, 0);
          tabLayout.addView(tab1);
      
          FrameLayout tab2 = (FrameLayout) LayoutInflater.from(this).inflate(R.layout.tab_cell,, null);
          tab2.findViewById(R.id.click_view).setOnClickListener(this);
          tab2.findViewById(R.id.click_view).setTag(Second_TAB);
          secondtab = (TextView) tab2.findViewById(R.id.tab_header);
          secondtab.setWidth(SMALL_TAB_WIDTH);
          secondtab.setText(R.string.roster);
          secondtab.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_selector_s, 0, 0);
          tabLayout.addView(tab2);
      
          FrameLayout tab3 = (FrameLayout) LayoutInflater.from(this).inflate(R.layout.tab_cell, null);
          tab3.findViewById(R.id.click_view).setOnClickListener(this);
          tab3.findViewById(R.id.click_view).setTag(Third_TAB);
          thirdtab = (TextView) tab3.findViewById(R.id.tab_header);
          thirdtab.setWidth(SMALL_TAB_WIDTH);
          thridtab.setText(R.string.copy_entry);
          thridtab.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.select_activity_selector_s, 0, 0);
          tabLayout.addView(tab3);
      

      tab_cell.xml 如下:

      <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content" android:layout_height="wrap_content">
          <TextView
              android:id="@+id/tab_header"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="45dp"
              android:layout_weight="1"
              android:gravity="center"
              android:maxLines="1"
              android:textSize="10.5sp"/>
          <RelativeLayout
              android:id="@+id/click_view"
              android:layout_width="40dp"
              android:layout_height="40dp"
              android:layout_gravity="center"></RelativeLayout>
      </FrameLayout>
      

      下面是代码中声明为tabLayout的activity中的线性布局:

       <LinearLayout
                              android:id="@+id/tabs" android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:orientation="horizontal"
                              android:weightSum="10"></LinearLayout>
      

      【讨论】:

      • 我已经添加了。请检查一下
      【解决方案5】:

      您在这张图片中看到的不是一个简单的按钮栏,它被称为底部导航栏,Android 对此有专门的代码。 In this question 你可以学习如何创建和使用它。当然,您可以使用简单的 XML 或 Java 代码更改颜色。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-10
        • 2016-11-04
        • 2019-02-05
        • 2015-09-22
        • 2011-10-20
        • 1970-01-01
        • 2013-09-05
        • 1970-01-01
        相关资源
        最近更新 更多