android - layout_widthPercent加载dependencies后还是不能使用,是什么原因?

查看:365
本文介绍了android - layout_widthPercent加载dependencies后还是不能使用,是什么原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

为什么添加依赖后,还是不能使用layout_widthPercent/layout_heightPercent?
依赖:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:percent:24.2.1'
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}

使用layout_widthPercent

<?xml version="1.0" encoding="utf-8"?>
<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">
    <Button
        android:id="@+id/button1"
        android:text="Button1"
        android:layout_gravity="left|top"
        android:layout_widthPercent="50%"
        android:layout_heightPercent="50%"/>
</android.support.percent.PercentFrameLayout>

告警:

 No resource identifier found for attribute 'layout_widthPercent' in package 'android'

解决方案

谢邀. 没有使用过 android.support.percent.PercentFrameLayout , 但是从 package 名字就大概知道它是support提供的, 非原生环境中所支持的Layout. 所以xml代码应该如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appCompat="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:text="Button1"
        android:layout_gravity="left|top"
        appCompat:layout_widthPercent="50%"
        appCompat:layout_heightPercent="50%"/>
</android.support.percent.PercentFrameLayout>

注意 appCompat 的定义和引用.
最后说一点, 但凡在代码里或者layout-xml能引用的Layout(or View), 都是可以正常使用的. 如果报错, 那就是用法用错了.

p.s. 如果答错, 请直接踩这个回答吧(因为我没具体使用过该PercentFrameLayout).

这篇关于android - layout_widthPercent加载dependencies后还是不能使用,是什么原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆