如何创建android按钮栏?

时间:2017-03-31 08:56:15

标签: android android-layout android-button

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

5 个答案:

答案 0 :(得分:4)

<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

的三个按钮

答案 1 :(得分:1)

BottomNavigationView 元素 布局中的

private static class MessageWrapper<T> {
    public T message;
}

RES /菜单/ my_navigation_items.xml:

 <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" />

答案 2 :(得分:0)

 <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>

答案 3 :(得分:0)

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的活动中的线性布局:

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

答案 4 :(得分:0)

您在该图片中看到的并不是一个简单的按钮栏,它被称为底部导航栏,而Android则具有特定的代码。 In this question您可以学习如何创建并使用它。您可以使用简单的XML或Java代码更改颜色。希望你觉得它有用。