我正在努力研究创建布局时是否可以使用百分比位置/尺寸。我想要的是这样的......
^
|
|
| 68%
|
|
v
Gallery (height equivalent of 16% total screen size)
^
| 16%
v
我在一台设备上进行测试,该设备在横向上具有800x480实际像素的显示,并且我正在使用以下内容强制显示...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="80px"
android:layout_marginTop ="320px"
/>
</RelativeLayout>
显然,我不想对固定的px
单元进行硬编码,但我不能将68%
或0.68
用于layout_marginTop
(例如)。我看过dp
个单位,但我不确定我是否可以这样做。
我必须承认UI设计是我的一个弱点,所以任何建议都会感激不尽。
编辑:如果有人正在寻找类似的答案,以备将来参考,按照艾伦摩尔的建议,我有以下工作正是我想要的......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/bground"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.68"
android:background="@android:color/transparent"
/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.16"
android:background="@android:color/transparent"
/>
</LinearLayout>
我设法找到了使用layout_weight
的其他一些示例,并决定将TextView
高度设置为0dp
,并使用浮点数作为权重。工作得很好。 :)
答案 0 :(得分:65)
我认为你想要的是设置android:layout_weight,
http://developer.android.com/resources/tutorials/views/hello-linearlayout.html
类似这样的东西(我只是将文本视图放在上面和下面作为占位符):
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="68"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="16"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="16"/>
</LinearLayout>
答案 1 :(得分:17)
<android.support.percent.PercentFrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
app:layout_heightPercent="68%"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
app:layout_heightPercent="16%"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</android.support.percent.PercentFrameLayout>
答案 2 :(得分:15)
对于TextView及其后代(例如Button),您可以从WindowManager获取显示大小,然后将TextView高度设置为其中的一小部分:
Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
btn.setHeight((int)(display.getHeight()*0.68));
答案 3 :(得分:5)
上述问题也可以通过指南使用ConstraintLayout来解决。
以下是摘录。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:id="@+id/upperGuideLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.68" />
<Gallery
android:id="@+id/gallery"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/lowerGuideLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/upperGuideLine" />
<android.support.constraint.Guideline
android:id="@+id/lowerGuideLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.84" />
</android.support.constraint.ConstraintLayout>
答案 4 :(得分:1)
看看这个:
http://developer.android.com/reference/android/util/DisplayMetrics.html
你可以获得屏幕的高度,并且计算68%的屏幕是简单的数学计算。