Android-ScrollView和PercentRelativeLayout
我在将android.support.percent.PercentRelativeLayout与ScrollView组合时遇到问题。下面的xml文件Android-ScrollView和PercentRelativeLayout,android,android-scrollview,android-relativelayout,android-percentrelativelayout,Android,Android Scrollview,Android Relativelayout,Android Percentrelativelayout,我在将android.support.percent.PercentRelativeLayout与ScrollView组合时遇到问题。下面的xml文件 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/ScrollView01" android:layout_wi
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="none">
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="5%"
app:layout_marginLeftPercent="5%"
android:src="@drawable/o"
android:id="@+id/imageView3" />
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="30%"
app:layout_marginLeftPercent="5%"
android:src="@drawable/o"
android:layout_below="@+id/imageView3"
android:layout_alignStart="@+id/imageView3"
android:layout_marginTop="151dp" />
</android.support.percent.PercentRelativeLayout>
使用RelativeLayout,效果很好。
有人知道问题出在哪里吗?谢谢
编辑:如果我将ScrollView高度设置为1000dp,它也可以正常工作,但我希望高度与元素的马赫数相同
我想学习如何使用宽度、高度和边距的百分比选项,以便像在html、css中一样使用它们。我想将宽度和高度的边距设置为%而不是dp
相信你能以这种方式在Android上完成所有的布局会让你陷入困境。如果您正在学习Android中的UI布局,第一步就是忘记所有关于HTML布局的知识。HTML主要是关于文档的。一个好的移动设备UI不应该看起来像文档
让我们举一个非常简单的例子:一个按钮。假设您想在布局中放置一个按钮。它应该有多大?嗯,它应该足够大,可以显示按钮的整个文本。它也不应该占用过多的空间
在设计布局时,您必须记住,您不知道用户的屏幕将有多大。因此,如果你说,“我想让我的按钮宽度占屏幕的20%”,在某些设备上,你可能没有足够的空间来显示按钮的整个文本。在其他屏幕上,您可能会有一个按钮,其空间令人尴尬,其中文本只占实际按钮的很少一部分
至于利润,谷歌用户界面指南建议网格间距为16dp单位。所以在手机上,一个典型的利润率是16dp。在有更多空间的平板电脑上,48dp的余量将更合适。使用百分比指定边距将在小型设备上创建压缩布局,并在大型布局上浪费空间
您必须了解三个主要的尺寸概念:
以密度无关像素(dp)为单位的绝对测量。这通常用于边距和填充
匹配父项
。我希望我的组件占据父组件的整个宽度/高度
包装内容
。我希望我的组件只占用它需要适当显示的宽度/高度,而不是更多。通常,这仅用于宽度或高度,但不能同时用于宽度和高度。大多数小部件大小调整算法要求至少有一面是“已知”大小,以便确定它在另一个维度中需要多少空间
仅使用这三种大小,您就可以完成数量惊人的UI布局。纽扣的最佳尺寸是多少?在手机上,在纵向模式下,android:width=“match\u parent”
&android:height=“wrap\u content”
。取手机的整个宽度,但将高度限制为刚好足以显示所有按钮文本。太好了
当然,最终你会想要一个两个组件并排的布局。现在你做什么?这是一个使用LinearLayout
并使用android:layout\u weight
属性表示:我希望这两个组件具有相同的宽度(即屏幕宽度的50%)
然后,您可能会尝试为更大的设备(如平板电脑)进行更复杂的布局。这是PercentRelativeLayout
最合适的位置PercentageRelativeLayout
是一个专门的容器,用于处理一些复杂的布局
如果您想要一个具有两个垂直图像和较小屏幕滚动条的布局,我将使用以下内容:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:src="@drawable/image1"
android:adjustViewBounds="true" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image2"
android:adjustViewBounds="true" />
</LinearLayout>
</ScrollView>
滚动容器中的两个图像与设备一样宽
说到ImageView
,有一个技巧需要知道如何调整大小android:adjustViewBounds=“true”
的意思是:由于我的高度是“包裹内容”(即灵活的),所以要使我的高度保持宽高比
我建议您阅读有关UI布局的Android教程,并查看示例项目,了解如何在Android中完成布局
伙计,我明白了。当我第一次学习Android布局时,我很困惑地发现没有标准的百分比类型大小。但随着我在Android上做越来越多的布局,我开始了解组件大小是如何工作的,并在这方面做得更好。然后,我开始编写自定义组件,使用Android内置的功能实现自己的大小调整算法
只要坚持下去,在您使用布局一段时间后,一切都会有意义。好的,它应该是什么样子的?而且,如果RelativeLayout
工作正常,为什么不使用它呢?我想使用百分比选项,但我不能使用相对布局,你知道我可以这样做的方法吗?例如,对于RelativeLayout应用程序:layout_widthPercent=“50%”不起作用。第二幅图像应该与第一幅图像具有相同的宽度和高度,并且应该较低,但它仍保留在手机的第一个屏幕上,并且不会降低,因此用户可以通过滚动查看它。谢谢克里斯!:)您是否希望第二个图像位于屏幕下方,因此用户必须滚动才能看到它?请您描述一下您的应用程序,以及这些图像显示时发生的情况?顺便说一句,如果您希望第二个图像位于屏幕下方,那么,RelativeLayout
对你没有多大帮助。我有一些想法,但首先我需要确保我了解你想要实现的目标。