簡體   English   中英

Android-使6個ImageButtons適合布局

[英]Android - Fitting 6 ImageButtons to layout

在一個屏幕上安裝6個圖像按鈕時,我一直遇到很大的問題。

我已經嘗試了Intellij和ADT,但一無所獲。 我還使用了兩種方法並圍繞它們進行游戲。 其中之一是將線性布局插入相對布局,並將圖像按鈕插入其中。 首先,這看起來不錯,但是,在大屏幕上我的效果不佳。 另一種方法是在xml中使用常規對齊方法,但這給我帶來了更糟糕的結果,並且我現在真的陷入困境。 我想我會采用第一種方法,但是我想獲得良好的結果,因為這是我的高級項目。 您可以在下面找到兩種方法。 您能幫我做錯什么嗎? 非常感謝你。

- -編輯 - -

我的意圖是3行,每行2個按鈕。 屏幕截圖如下。 我有nexus 7 2013和galaxy s2,結果與以下結果相似,因此我認為以下結果正確。

這是第一種方法的圖片: 方法1

這是第二種方法的圖片: 方法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"
        />

簽出此代碼,如果可行,請發表評論

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

您將要對線性布局使用第一種方法,並增加權重,以免它被分離出來。 這是您修改后的代碼,應該可以解決該問題(您將希望為每個設備創建更大的圖像,以便看起來也更加干凈)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM