0

我制作了一个自定义主题来在操作栏上显示我的图像按钮。我想知道如何在android中垂直居中这些按钮。

到目前为止我写的代码。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|left|center_vertical"
        android:background="@drawable/groups" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center|fill_vertical"
        android:background="@drawable/contactlist" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right|center_vertical"
        android:background="@drawable/favourites" />

</FrameLayout>

这就是我要的

在此处输入图像描述

4

3 回答 3

0

您可以将图像按钮放在LinearLayout垂直方向的内部,然后将它们的重力设置为android:layout_gravity="center"

于 2013-09-19T03:38:52.090 回答
0
// try this
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:layout_weight="1">

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/groups" />

        </LinearLayout>
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/white"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:layout_weight="1">

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/contactlist" />

        </LinearLayout>
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/white"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:layout_weight="1">

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/favourites" />

        </LinearLayout>
    </LinearLayout>
</FrameLayout>
于 2013-09-19T04:09:35.130 回答
0

请遵循此代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageButton
        android:layout_width="0dip"
        android:layout_height="50dip"
        android:layout_gravity="top|left|center_vertical"
        android:layout_weight="1"
        android:background="#FE6602" />

    <ImageButton
        android:layout_width="0dip"
        android:layout_height="50dip"
        android:layout_gravity="top|center|fill_vertical"
        android:layout_weight="1"
        android:background="#DBEAF9" />

    <ImageButton
        android:layout_width="0dip"
        android:layout_height="50dip"
        android:layout_gravity="top|right|center_vertical"
        android:layout_weight="1"
        android:background="#FAF2B0" />

</LinearLayout>

变化:

android:layout_height="50dip" to  android:layout_height="wrap_content"

android:background="#FE6602" to android:background="@drawable/contactlist"

我只是把简单的颜色和静态尺寸或测试目的工作像魅力。

于 2013-09-19T04:37:57.417 回答