簡體   English   中英

Android ImageButton在不同的屏幕

[英]Android ImageButton in different screens

在我的應用程序的UI中,我有5個ImageButton在3.7“屏幕上都可以,但是如果我有2.7”,3.0“或其他屏幕,我的UI將無法縮放並且看起來很糟糕。看起來像這樣:

3,7英寸

在此處輸入圖片說明

2,7“

在此處輸入圖片說明

如何在所有屏幕上都擁有漂亮的外觀,就像在3.7“屏幕上一樣?

程式碼:main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/two"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp"
        android:orientation="vertical"
        android:scaleType="fitXY" >

        <ImageButton
            android:id="@+id/ibTest1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/one" />

        <ImageButton
            android:id="@+id/ibTest2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/one" />

        <ImageButton
            android:id="@+id/ibTest3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/one" />

        <ImageButton
            android:id="@+id/ibTest4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/one" />

        <ImageButton
            android:id="@+id/ibTest5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/one" />
    </LinearLayout>

</RelativeLayout>

使用RelativeLayout ,然后放置一個高度為wrap_contentLinearLayout並將按鈕放在該LinearLayout內。 還要使用dp而不是px作為尺寸。

PS:如果您發布布局代碼,我們可以為您提供更好的幫助。

編輯:現在,我可以看到您的代碼,可以向您展示我想要的意思:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/two"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_centerInParent="true" >

        <ImageButton
            android:id="@+id/ibTest1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/one" />

        <ImageButton
            android:id="@+id/ibTest2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/one" />

        <ImageButton
            android:id="@+id/ibTest3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/one" />

        <ImageButton
            android:id="@+id/ibTest4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/one" />

        <ImageButton
            android:id="@+id/ibTest5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/one" />
    </LinearLayout>

</RelativeLayout>

你能提供一些代碼嗎? 也許嘗試添加到XML

android:scaleType="fitXY"

首先為所有人制作圖像

mdpi,hdpi,ldpi

根據他們的大小。

僅將Buttonat置於Lineatlayout的中心。

然后設置屬性gravity =“ Center”;

確保您使用的是dp不是使用'px'這是一個常見原因。

嘗試在xml中的每個imageview中添加

  android:adjustViewBounds="true"
  android:scaleType="centerCrop"

暫無
暫無

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

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