Android - 安装6个ImageButtons进行布局

时间:2014-05-19 03:18:23

标签: android xml android-layout intellij-idea

我在将6个图像按钮安装到一个屏幕上时遇到了很大的问题。

我尝试过Intellij和ADT,但没有想到什么。我也用过两种方法来玩它们。其中之一是将线性布局插入相对布局并在其中插入图像按钮。这似乎是第一位的,然而,我在大屏幕上得到了糟糕的结果。另一种方法是在xml中使用常规对齐方法,但这给了我更糟糕的结果,我现在真的被卡住了。我想我会选择第一种方法,但我希望得到一个好的结果,因为这是我的高级项目。您可以在下面找到这两种方法。你能帮我解决我做错的事吗?非常感谢你。

--- --- EDIT

我的意图是3行,每行2个按钮。屏幕截图如下。我有nexus 7 2013和galaxy s2,结果与下面的结果相似,所以我假设下面的结果是正确的。

这是第一种方法的图片: Method 1

这是第二种方法的图片: Method 2

---编辑结束---

第一种方法(在相对布局中将图像按钮插入3个线性布局):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="390dp"
        android:layout_alignParentBottom="true" android:layout_alignParentStart="true">

    <ImageButton
        android:id="@+id/apuButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/apu"
        android:scaleType="fitCenter"
        />
    <ImageButton
        android:id="@+id/lmsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/lms"
        android:scaleType="fitCenter"
        />

</LinearLayout>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true" android:layout_alignParentStart="true"
        android:layout_marginTop="140dp" android:layout_marginBottom="255dp" android:id="@+id/linearLayout">

    <ImageButton
        android:id="@+id/mailButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/mail"
        android:scaleType="fitCenter"/>

    <ImageButton
            android:id="@+id/scaButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/sca"
            android:scaleType="fitCenter"/>

</LinearLayout>
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="119dp" android:layout_alignTop="@+id/linearLayout"
        android:layout_alignParentStart="true" android:layout_marginBottom="140dp" android:id="@+id/linearLayout2">

    <ImageButton
            android:id="@+id/libraryButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/library"
            android:scaleType="fitCenter"/>

    <ImageButton
            android:id="@+id/ratingButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/rating"
            android:scaleType="fitCenter"/>

</LinearLayout>
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignTop="@+id/linearLayout2" android:layout_alignParentEnd="true"
        android:layout_marginTop="127dp">
</LinearLayout>

第二种方法(使用路线):

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
        android:id="@+id/apuButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/apu"
        android:scaleType="fitCenter"


        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
<ImageButton
        android:id="@+id/lmsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/lms"

        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />



<ImageButton
        android:id="@+id/mailButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/mail"
        android:scaleType="fitCenter"

        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        />

<ImageButton
        android:id="@+id/scaButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/sca"
        android:scaleType="fitCenter"

        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />

<ImageButton
        android:id="@+id/libraryButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"

        android:src="@drawable/library"
        android:scaleType="fitCenter"

        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        />
<ImageButton
        android:id="@+id/ratingButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/rating"
        android:scaleType="fitCenter"

        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        />

3 个答案:

答案 0 :(得分:0)

查看此代码,如果有效,请发表评论

 <LinearLayout
    android:id="@+id/grd_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="33"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/apuButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/lmsButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="33"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/mailButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/scaButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="33"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/libraryButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/ratingButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

// Try this way,hope this will help you to solve your problem...

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ImageButton
            android:id="@+id/apuButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"
            android:scaleType="fitXY" />
        <ImageButton
            android:id="@+id/lmsButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"
            android:scaleType="fitXY"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ImageButton
            android:id="@+id/mailButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"
            android:scaleType="fitXY"/>

        <ImageButton
            android:id="@+id/scaButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"
            android:scaleType="fitXY"/>

    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ImageButton
            android:id="@+id/libraryButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"
            android:scaleType="fitXY"/>

        <ImageButton
            android:id="@+id/ratingButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"
            android:scaleType="fitXY"/>

    </LinearLayout>
</LinearLayout>

答案 2 :(得分:0)

您需要使用线性布局的第一种方法并添加权重,以便它不会像这样分离出来。以下是修改后的问题的修改后的代码(您需要为每个设备创建更大的图像,以便它看起来更清晰一些)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity$PlaceholderFragment" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_alignParentStart="true"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/apuButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/apu" />

    <ImageButton
        android:id="@+id/lmsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/lms" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_alignParentStart="true"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/mailButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/mail" />

    <ImageButton
        android:id="@+id/scaButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/sca" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_alignParentStart="true"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/libraryButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/library" />

    <ImageButton
        android:id="@+id/ratingButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/rating" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="127dp"
    android:layout_weight="1"
    android:orientation="horizontal" >
</LinearLayout>