簡體   English   中英

使Android活動適合所有屏幕尺寸

[英]Make android activity fit all screen sizes

這是我的活動的XML文件。

如何使它適合所有屏幕尺寸,而不會弄亂圖像按鈕!

我正在使用相對布局 ,應該使用線性布局嗎?

以下是我的代碼。

<RelativeLayout 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: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=".MainActivity" 
android:background="@drawable/good">

<ImageButton
    android:id="@+id/ImageButton03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton1"
    android:layout_alignTop="@+id/ImageButton01"
    android:background="@drawable/btn_blue"
    android:src="@drawable/plus" 
    android:onClick="NewCategory"/>

<ImageButton
    android:id="@+id/ImageButton01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/ImageButton02"
    android:layout_below="@+id/ImageButton02"
    android:layout_marginTop="29dp"
    android:background="@drawable/btn_blue"
    android:src="@drawable/plus"
    android:onClick="NewItem" />

<ImageButton
    android:id="@+id/ImageButton04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ImageButton01"
    android:layout_alignLeft="@+id/ImageButton03"
    android:background="@drawable/btn_blue"
    android:src="@drawable/call" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="سـوبر ماركت التنـور"
    android:textSize="40dp"
    android:background="@drawable/btn_red"
    android:textColor="@color/white" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="24dp"
    android:background="@drawable/btn_blue"
    android:onClick="ViewAllCategories"
    android:src="@drawable/cart" />

<ImageButton
    android:id="@+id/ImageButton05"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ImageButton02"
    android:layout_alignLeft="@+id/button1"
    android:background="@drawable/btn_blue"
    android:onClick="CreateNewUserAccount"
    android:src="@drawable/user" />

<ImageButton
    android:id="@+id/ImageButton02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/ImageButton05"
    android:layout_below="@+id/imageButton1"
    android:layout_marginTop="33dp"
    android:background="@drawable/btn_blue"
    android:onClick="joinfacebook"
    android:src="@drawable/f" />

我認為您必須創建每個對象並以編程方式添加到布局中,並對每個對象使用比率。 例如:屏幕的比例為9/16。

高度= size.y; 寬度=比率*高度;

每個對象都將設置布局參數或邊距參數,它們應遵循此寬度和高度。

例如,使用您的一個按鈕:

imageButton1 = new ImageView(this);
imageButton1.setBackgroundResource(R.drawable.abc);
imageButton1.setLayoutParams(new LayoutParams(width/100, height/100));
marginParams = new ViewGroup.MarginLayoutParams(imageButton1.getLayoutParams());
marginParams.setMargins(width/24, height/12, 0, 0);
layoutParams = new RelativeLayout.LayoutParams(marginParams);
imageButton1.setLayoutParams(layoutParams);
layout.addView(imageButton1);

您必須為大屏幕編寫xml代碼。 我認為您將xml文件保留在res/layout文件夾中。 如果是這樣,您可以保留三個文件夾來區分各種屏幕,例如res/layout-smallres/layoutres/layout-long 在這三個文件夾中,可以為各種屏幕類型設置不同的對齊方式。 對於中型屏幕,將您的xml代碼保留在布局中,對於小屏幕-將其保留在layout-small等中,並在您的AndroidManifest.xml設置...

<supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens="true"        
    android:anyDensity="true" />

不要使用相對布局。 使用帶有weightSum線性布局。

它可以在大多數設備上工作。 對於平板電腦,您必須在layout-sw600dp,layout-sw720dp文件夾中重新設計新的布局。

重量樣本

這是設計多種屏幕尺寸的准則

更多Extra detils是Google I / O App下載代碼,並進行檢查。

我更喜歡先獲取屏幕尺寸,然后以編程方式在代碼中進行所有邊距和尺寸設置。 工作更多,但控制方式更多。
只是一個例子,這種方式適用於您擁有的所有內容,文本大小,視圖大小,位置...:

    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    int displayHeight = metrics.heightPixels;
    int displayWidth = metrics.widthPixels;
    float scaledDensity = metrics.scaledDensity;

    float percentageToMoveViewDown = (float) 20.0;
    float viewY_float = (float) ((displayHeight / 100.0) * percentageToMoveViewDown);
    int viewY_int = Math.round(viewY_float);

    RelativeLayout.LayoutParams view_Layout_params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    view_Layout_params.topMargin = viewY_int;
    view.setLayoutParams(view_Layout_params);

最好的和最簡單的方法是右鍵單擊android studio中的res文件夾,然后在資源管理器中按show

在res中使用以下名稱創建四個不同的文件夾:

layout-small

layout-normal

layout-large

layout-xlarge

注意:完成此操作后,請確保您的xml位於具有正確文件夾名稱的四個文件夾中,並根據您的喜好進行了修改

刪除layout並保留其他四個

並將您的xml放在四個中,然后根據大小對每個xml進行修改

現在終於在您的AndroidManifest添加以下內容

    <supports-screens
    android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true" />

暫無
暫無

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

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