【发布时间】: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",布局会以某种方式被破坏。但是,如果我改变方向,这不会使布局成比例。任何帮助将不胜感激。
谢谢。
【问题讨论】:
-
你应该阅读
LinearLayout和weight! -
如果你想要 150dp 的布局而不是你为什么要为其他布局使用权重概念?
-
您好,非常感谢您的提示。我现在发现了问题。我将它们设置为
android:layout_height="fill_parent",但忘记设置weight。对不起,初学者的错误。
标签: android android-layout orientation screen-size