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