eclipse Android - 屏幕的布局宽度/高度百分比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25064073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - layout width/height percentage of a screen?
提问by user3422952
I was testing out B4A (Basic4Android) and there's something called a "designer script", this basically allows you to position elements on a screen.
For example:TextBox1.setTopAndBottom(0%y, 50%y)
TextBox1.setLeftAndRight(0%x, 50%x)
我正在测试 B4A ( Basic4Android) 并且有一种叫做“设计器脚本”的东西,它基本上允许您在屏幕上定位元素。例如:TextBox1.setTopAndBottom(0%y, 50%y)
TextBox1.setLeftAndRight(0%x, 50%x)
When this script runs it will automatically position the TextBox1 on the screen, imagine the the screen is 100 pixels by 100 pixels (100x100), the TextBox1 will be placed at (0, 0) (top left), (50, 50) (bottom right).
当这个脚本运行时,它会自动将 TextBox1 定位在屏幕上,假设屏幕是 100 x 100 像素 (100x100),TextBox1 将被放置在 (0, 0) (左上角), (50, 50) (右下)。
How can I achieve something like this in Eclipse?
I can't figure it out. I want to position an element (TextBox1 for example) to fit 25% of the width and 50% of the height for example. How do I achieve this?
我怎样才能在 Eclipse 中实现这样的目标?
我想不通。我想定位一个元素(例如 TextBox1)以适应例如宽度的 25% 和高度的 50%。我如何实现这一目标?
采纳答案by Phant?maxx
OK. This is the trick
好的。这是诀窍
In simple words: make a 1px (px, not dp- you don't want it scaled, but small enough to be trascurable!) TextView which will be your invisible(you leave it transparent and set no text in it) "center of the universe".
Then stretch your other TextView, but limit it to stay to the left(which is at 50% - 1/2 px) of the center and above(again, 50% - 1/2 px) it:
在简单的话:做一个1px的(PX,没有DP-你不希望它缩放,但是小到足以成为trascurable!)TextView的,这将是你看不见的(你离开它透明,并在其中设置没有文本)“的中心宇宙”。
然后拉伸你的另一个 TextView,但限制它停留在中心的左侧(50% - 1/2 像素)和上方(再次,50% - 1/2 像素):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/centerPoint"
android:layout_width="1px"
android:layout_height="1px"
android:layout_centerInParent="true"
/>
<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@id/centerPoint"
android:layout_above="@id/centerPoint"
/>
</RelativeLayout>
回答by Mark McDonald
Consider using the Android percent support library, which adds a PercentRelativeLayoutand PercentFrameLayoutthat allow children to define their sizing in percentages.
考虑使用Android 百分比支持库,它添加了一个PercentRelativeLayout和PercentFrameLayout,允许孩子们定义他们的百分比大小。
e.g.
例如
<android.support.percent.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentFrameLayout/>
回答by ashishduh
You need to use LinearLayout and weights.
您需要使用 LinearLayout 和权重。
http://developer.android.com/guide/topics/ui/layout/linear.html
http://developer.android.com/guide/topics/ui/layout/linear.html
Example of 6 TextViews, each row will take up 50% of the screen height, and within each row each TextView will take up 33% of the screen width.
6个TextView的例子,每行将占据屏幕高度的50%,每行内每个TextView将占据屏幕宽度的33%。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/text4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/text5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/text6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>