Android - ScrollView和PercentRelativeLayout

Ira*_*ris 1 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_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>
Run Code Online (Sandbox Code Playgroud)

打印这个 不是我想要的

使用RelativeLayout它工作正常.有谁知道问题出在哪里?谢谢!

编辑:如果我将ScrollView高度设置为1000dp它也工作正常,但我希望高度像元素一样高

kri*_*son 5

我想学习如何使用宽度高度和边距的百分比选项,以便我可以像在html,css中一样使用它们.我希望以%为单位设置边距,而不是在dp中为宽度和高度设置边距.

相信你能以这种方式在Android中完成所有布局会让你陷入困境.如果您正在学习Android中的UI布局,那么第一步就是忘记有关HTML布局的所有知识.HTML主要是关于文档.一个好的移动设备UI看起来不应该像文档.

我们来看一个非常简单的例子:一个按钮.假设您想在布局中放置一个按钮.它应该有多大?好吧,它应该足够大,以显示按钮的整个文本.它也不应该占用太多空间.

在设计布局时,必须记住,您不知道用户屏幕的大小.因此,如果你说"我想让我的按钮宽度为屏幕的20%",在某些设备上你可能没有足够的空间来显示按钮的整个文本.在其他屏幕上,您可能会有一个带有令人尴尬空间的按钮,其中文本占用的实际按钮非常少.

至于边距,Google UI指南推荐16dp单位的网格间距.所以在手机上,典型的保证金将是16dp.在你有更多空间的平板电脑上,48dp的保证金将更合适.使用百分比指定边距将在小型设备上创建打包布局,在大型布局上创建浪费空间.

您必须了解三个主要的大小概念:

  1. 绝密测量与密度无关的像素(dp).这通常用于边距和填充.

  2. match_parent.我希望我的组件占用父组件的整个宽度/高度.

  3. wrap_content.我希望我的组件只占用它需要适当显示的宽度/高度,而不是更多.通常,这仅用于宽度或高度,但不能用于两者.大多数小部件大小调整算法要求至少一侧是"已知"大小,以便确定在另一个维度中需要多少空间.

只需使用这三种尺寸,您就可以进行大量的UI布局.按钮的最佳尺寸是多少?在手机上,在纵向模式下,android:width="match_parent"&android:height="wrap_content".取整个手机的宽度,但将高度限制到足以显示所有按钮文字.完善.

当然,最终你会希望有一个并排的两个组件的布局.那你现在怎么办?这是一个使用a LinearLayout和使用android:layout_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>
Run Code Online (Sandbox Code Playgroud)

滚动容器中的两个图像与设备一样宽.

当涉及到时ImageView,有一个关于尺寸的技巧. android:adjustViewBounds="true"意思是:因为我的身高是"wrap_content"(即灵活的),所以要使我的身高保持纵横比与宽度.

我建议您浏览有关UI布局的Android教程,并查看示例项目以了解如何在Android中完成布局.

老兄,我明白了.当我第一次学习Android布局时,我很惊讶地看到没有标准的百分比型大小.但随着我在Android中做了越来越多的布局,我开始了解组件大小如何工作并且在它上面变得更好.然后我开始编写自定义组件,使用Android内置的功能实现自己的大小调整算法.

坚持下去,在你使用布局一段时间后,这一切都会有意义.