簡體   English   中英

無論屏幕大小和方向如何,如何使Android布局成比例

[英]How to make Android layout proportional regardless of screen size and orientation

這是我在這里的第一篇文章。 我剛剛學習了Android編程並遇到了這個問題。 我搜索了一下,發現了一些解決方案,但我無法使我的代碼工作。 所以我想知道我的代碼或實現的哪一部分是錯誤的。

所以,問題是我想制作一個在不同屏幕尺寸和方向上保持相同比例的布局。 這是我的代碼:

activity_main.xml中

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:baselineAligned="false" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="150dp" > <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:gravity="left" android:id="@+id/textView1"/> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:gravity="right" android:id="@+id/textView2"/> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:id="@+id/textView3"/> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:gravity="left" android:id="@+id/textView4"/> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:gravity="center" android:id="@+id/textView5"/> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent" android:gravity="right" android:id="@+id/textView6"/> </LinearLayout> <SeekBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/seekBar" android:max="255"/> </LinearLayout> 

我目前無法發布截圖,因為它說“發布圖片需要至少10個聲望。”

因此,我希望將屏幕垂直划分為3個部分,並將頂部水平分割為2,將底部水平分為3,並保持其比例,無論屏幕尺寸,分辨率和方向如何。

唯一不起作用的部分是頂部塊,在我將其設置為android:layout_height="150dp"的代碼android:layout_height="150dp"因為如果我將它們設置為android:layout_height="fill_parent" ,布局會以某種方式被破壞android:layout_height="fill_parent" 但是,如果我改變方向,這不會使布局成比例。 任何幫助將不勝感激。

謝謝。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#555555">

   <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:orientation="horizontal"
       android:background="#555555">
       <TextView 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="part1"
           android:background="#008000"/>
       <TextView 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="part2"
           android:background="#0000FF"/>
   </LinearLayout>
   <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:background="#ffffff"></LinearLayout>
   <LinearLayout 
       android:layout_width="match_parent"
       android:orientation="horizontal"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:background="#000000">
       <TextView 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="part1"
           android:background="#008000"/>
       <TextView 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="part2"
           android:background="#A9F114"/>
       <TextView 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="part3"
           android:background="#B4155D"/>
   </LinearLayout>
</LinearLayout>

在我這樣做時,似乎人們已經回答了你的問題。 在此輸入圖像描述 盡管如此,如果您仍需要幫助,我會發布此答案。 此圖像包含您需要分配給布局的權重,以圖形方式表示。 請原諒一些缺乏對齊,我匆匆做了這件事。

通過在三個linearLayout上使用layout_weight並為每個都分配相同的值,無論屏幕大小如何,屏幕都將垂直分為3。 另外,將高度指定為0dpandroid:layout_height="0dp"

完整的xml將是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="left" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="right" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="left" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="right" />
    </LinearLayout>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255" />

</LinearLayout>

您也可以實際獲得屏幕尺寸,並根據您的要求設置高度/重量。

Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics ();
    display.getMetrics(outMetrics);

    float density  = getResources().getDisplayMetrics().density;
    float dpHeight = outMetrics.heightPixels / density;
    float dpWidth  = outMetrics.widthPixels / density;

暫無
暫無

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

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