【问题标题】:How to make Android layout proportional regardless of screen size and orientation无论屏幕大小和方向如何,如何使 Android 布局成比例
【发布时间】:2015-05-27 10:54:44
【问题描述】:

这是我在这里的第一篇文章。我刚刚学习了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="fill_parent",布局会以某种方式被破坏。但是,如果我改变方向,这不会使布局成比例。任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 你应该阅读LinearLayoutweight
  • 如果你想要 150dp 的布局而不是你为什么要为其他布局使用权重概念?
  • 您好,非常感谢您的提示。我现在发现了问题。我将它们设置为android:layout_height="fill_parent",但忘记设置weight。对不起,初学者的错误。

标签: android android-layout orientation screen-size


【解决方案1】:
<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>

【讨论】:

    【解决方案2】:

    在我做这个的时候,似乎人们已经回答了你的问题。 尽管如此,如果您仍然需要帮助,我会发布此答案。此图像包含您需要分配给布局的权重,以图形方式表示。请原谅我没有对齐,这是我匆忙做的。

    【讨论】:

      【解决方案3】:

      通过在三个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>
      

      【讨论】:

        【解决方案4】:

        您也可以务实地获取屏幕尺寸并根据您的要求设置高度/重量。

        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;
        

        【讨论】:

          猜你喜欢
          • 2020-05-20
          • 1970-01-01
          • 2021-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-20
          • 2014-12-27
          相关资源
          最近更新 更多