标签: android-percentrelativelayout

PercentRelativeLayout更具Performant?

我总是使用LinearLayout和RelativeLayouts,而我正在阅读"新" PercentRelativeLayout.我有几个问题:

  • 我应该总是开始使用这个或仅在线性布局的嵌套权重的情况下?

  • 这比其他人更有效吗?

有关PercentRelativeLayout的任何其他信息将受到欢迎.

谢谢!

performance android android-support-library android-percentrelativelayout

19
推荐指数
1
解决办法
1429
查看次数

ScrollView中的PercentRelativeLayout

我创建了一个使用ScrollViewa PercentRelativeLayout作为其子项的布局.它不适用于Lollipop和旧设备,但在Marshmallow设备上工作正常.请检查以下代码:

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.percent.PercentRelativeLayout
        android:id="@+id/scrollContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_heightPercent="100%"
        app:layout_widthPercent="50%">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_red_dark"
            android:text="kkjknadko"
            android:textColor="@android:color/black"
            app:layout_heightPercent="10%"
            app:layout_widthPercent="50%"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:text="Abcaad"
            android:textColor="@android:color/black"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="10%"
            app:layout_widthPercent="50%"/>

        <TextView
            android:id="@+id/textview3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textview2"
            android:background="@android:color/holo_red_dark"
            android:text="Abcd"
            android:textColor="@android:color/black"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="10%"
            app:layout_widthPercent="50%"/>

        <TextView
            android:id="@+id/textview4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textview3"
            android:text="Abcd"
            android:textColor="@android:color/black"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="10%"
            app:layout_widthPercent="50%"/>

        <TextView
            android:id="@+id/textview5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textview4"
            android:background="@android:color/holo_red_dark"
            android:text="Abcd"
            android:textColor="@android:color/black"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="10%"
            app:layout_widthPercent="50%"/>

        <TextView
            android:id="@+id/textview6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textview5"
            android:text="Abcd"
            android:textColor="@android:color/black"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="10%"
            app:layout_widthPercent="50%"/>

        <TextView
            android:id="@+id/textview7"
            android:layout_width="wrap_content" …
Run Code Online (Sandbox Code Playgroud)

android android-scrollview android-5.0-lollipop android-6.0-marshmallow android-percentrelativelayout

16
推荐指数
2
解决办法
1810
查看次数

如何在PercentRelativeLayout中使用layout_aspectRatio?

我尝试在视图上实现16:9的宽高比PercentRelativeLayout.

所以我把这行放在build.gradle文件中: compile 'com.android.support:design:23.0.1'

我用这个布局:

<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="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        app:layout_aspectRatio="178%"
        android:scaleType="centerCrop"/>

</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

问题:

  • Android Studio警告我:'layout_height' should be defined为了ImageView.
  • 当我运行我的项目时,我得到了:Error:(15) No resource identifier found for attribute 'layout_aspectRatio'.

那有什么不对?

android android-layout android-constraintlayout android-percentrelativelayout

13
推荐指数
3
解决办法
8653
查看次数

将LinearLayout和"百分比"边距居中

编辑:我已收到两个关于固定保证金的综合答案.虽然我已经决定使用固定边距而不是重量边距,但最初的问题仍未解决.

我想在Android中获得以下设计:

期望的布局

一个居中的垂直列表(TextViews,EditViews等),它留下大约10%的水平空间作为左/右边距,带有背景.

我尝试过但没有工作/部分工作的内容:

  • LinearLayout,vertical,作为顶级布局.如果重力设置为"居中",则背景将限制为布局的大小.另外,如何以这种方式设置百分比边距(宽度)?
  • RelativeLayout上的LinearLayout:背景有效,水平居中工作,权重不存在.
  • LinearLayout上的LinearLayout:背景工作,权重工作,水平居中将所有可用空间推向右侧.

(在最后两种情况下,我的Eclipse也抱怨其中一个布局是多余的.)

我没有发布代码,认为这更像是一个与原则相关的问题.实现这一目标的最佳方法是什么?

谢谢.

对应于最后一个测试用例的XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:weightSum="1.0"
    android:background="#013c57" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <!-- Stuff -->
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android margin centering android-layout android-percentrelativelayout

6
推荐指数
1
解决办法
9617
查看次数

Android - ScrollView和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它也工作正常,但我希望高度像元素一样高

android android-scrollview android-relativelayout android-percentrelativelayout

1
推荐指数
1
解决办法
2340
查看次数

无法让android studio识别出PercentageRelativeLayout

我正在尝试使用PercentRelativeLayout但gradle将无法找到相关的资源标识符.

这是我的布局(列表视图中的简单项)

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true">


    <ImageView
        app:layout_widhtPercent="30%"
        android:layout_height="wrap_content"
        android:id="@+id/projects_lv_item_image" />

    <TextView
        app:layout_widhtPercent="60%"
        android:layout_height="wrap_content"
        android:id="@+id/projects_lv_item_title" />
</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的builde.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.devlopers.yemima.kh1"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.android.support:percent:24.1.1'


}
Run Code Online (Sandbox Code Playgroud)

这是我从Android Studio获得的错误:

Error:(11) No resource identifier …
Run Code Online (Sandbox Code Playgroud)

android build.gradle android-percentrelativelayout

0
推荐指数
1
解决办法
54
查看次数