PercentRelativeLayout不起作用

时间:2016-06-29 09:18:54

标签: android android-layout

我尝试使用PercentRelativeLayout来获取我的小部件的比率。即使我更新了dependecies,Android Studio也不会识别该库。

这是我的gradle

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.cobaltsign.androidwidget"
    minSdkVersion 21
    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:23.4.0'
compile 'joda-time:joda-time:2.9.3'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.google.android.gms:play-services-appindexing:9.0.2'
compile 'com.makeramen:roundedimageview:2.2.1'}

Any idea ?

2 个答案:

答案 0 :(得分:2)

您必须在compile 'com.android.support:percent:23.3.0'中导入build.gradle。看一下这个例子:

 <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%"/>

答案 1 :(得分:0)

在我的情况下,我必须使用app:layout_widthPercent="100%"而不是android:layout_width="match_parent"才能使得比例为1:2的app:layout_aspectRatio="200%"

结果布局是:

<FrameLayout
    android:background="@color/black_25"
    app:layout_aspectRatio="200%"
    app:layout_widthPercent="100%" />