我们如何才能使用百分比值的android视图元素?像这样如何使用百分比的android布局?
<TextView
android:id="@+id/"
android:layout_width="75%"
android:layout_height="50%"/>
我们如何才能使用百分比值的android视图元素?像这样如何使用百分比的android布局?
<TextView
android:id="@+id/"
android:layout_width="75%"
android:layout_height="50%"/>
到目前为止,Android的支持库已在何处使用限制:
与RelativeLayout
android.support.percent.PercentRelativeLayout
与FrameLayout
android.support.percent.PercentFrameLayout
如何使用它?
好了,先确保包括依赖于 build.grade(Module: app)
dependencies {
compile 'com.android.support:percent:23.3.0'
}
然后转到您的XML布局在这个例子中(.MainActivity)
<android.support.percent.PercentRelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:text="Button"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
app:layout_widthPercent="30%"/>
<Button
android:id="@+id/button2"
android:text="Button 2"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button"
app:layout_widthPercent="60%"/>
<Button
android:id="@+id/button3"
android:text="Button 3"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
app:layout_widthPercent="90%"/>
</android.support.percent.PercentRelativeLayout>
。详细请here:
谷歌有一个名为android.support.percent
例新的API PercentRelativeLayout
<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">
<TextView
android:id="@+id/myTextView"
app:layout_widthPercent="75%"
app:layout_heightPercent="50%"/>
</android.support.percent.PercentFrameLayout>
这里有一些错误。你的开始和结束标签是不同的(相对/框架),但更重要的是,这些属性是错误的,它应该是'app:layout_widthPercent =“75%”app:layout_heightPercent =“50%”' –
yup编辑它是类型错误因为我试图复制OP的例子 –
支持库只是增加了一个百分比的布局。
,它可以像这样
<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>
https://developer.android.com/reference/android/support/percent/PercentFrameLayout.html
得到它了。谢谢你@ chris-thoma –
做您可以使用PercentRelativeLayout, 这是最近无证除了Design Support Library,能够指定相对于彼此不仅元素的能力而且还包括可用空间的总百分比。
结构
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:src="@mipmap/ic_launcher"
android:background="#cecece"/>
<TextView
android:id="@+id/textview1"
android:layout_below="@id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_marginStartPercent="25%"
app:layout_marginEndPercent="25%"
android:text="PercentRelativeLayout example"
android:background="#bebebe"/>
</android.support.percent.PercentRelativeLayout>
的百分比包提供API来支持你的应用程序添加和管理基于百分比尺寸。
要使用,您需要把这个库添加到列表your Gradle dependency
:
dependencies {
compile 'com.android.support:percent:22.2.0'
// or compile 'com.android.support:percent:23.1.0'
}
为了演示目的,你可以访问的Git android-percent-support-lib-sample。
您可以使用PercentRelativeLayout
它是RelativeLayout
的一个子类,它支持基于百分比的尺寸和边距。
要使用,你需要把这个库添加到您的Gradle dependency
:
dependencies {
compile 'com.android.support:percent:23.1.0'
}
样品结构
<android.support.percent.PercentRelativeLayout
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.PercentRelativeLayout>
由于在第26.0.0版中将PercentFrameLayout
和PercentRelativeLayout
都删除,所以您需要t o使用ConstraintLayout
来调整TextView的百分比值。
ConstraintLayout
是构建适用于Android平台的响应式UI的强大工具,您可以在此处找到更多详细信息Build a Responsive UI with ConstraintLayout。
这里的例子,如何评估您的TextView的使用ConstraintLayout(W = 75%,H = 50%):
<android.support.constraint.ConstraintLayout
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">
<android.support.constraint.Guideline
android:id="@+id/ver_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75"/>
<android.support.constraint.Guideline
android:id="@+id/hor_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
app:layout_constraintBottom_toTopOf="@+id/hor_guideline"
app:layout_constraintEnd_toStartOf="@+id/ver_guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
不要忘了constraint-layout
依赖添加到您的模块build.gradle
文件
dependencies {
...
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
...
}
或修改您的布局直接在布局编辑器,如下图所示:
因此,您的TextView
宽度为父母的宽度和高度的75%是父母身高的50%:
除了在回答中引用的库中,你可以使用'机器人:layout_weight'在LinearLayout中实现类似的目标:https://github.com/commonsguy/cw-omnibus/blob/master/Containers/LinearPercent/res/layout/main.xml – CommonsWare
@CommonsWare但我们怎么能在单一布局中设置宽度和高度百分比? – Cyanotis
@Cyanotis:不,除了通过'android:layout_weight'沿着两个轴嵌套'LinearLayout'容器和控制百分比。毫无疑问,新的'android.support.percent'类更加灵活。我只是想指出经典的方法,因为有些人反对使用库。 – CommonsWare