相关疑难解决方法(0)

466
推荐指数
5
解决办法
19万
查看次数

错误:未在包'com.google.example'main.xml中找到属性'adSize'的资源标识符

当我按照说明通过xml将广告添加到我的应用中时,出现以下错误:

Description Resource Path Location Type
error: No resource identifier found for attribute 'adSize' in package 'com.google.example'  main.xml    /HelloWorld/res/layout  line 12 Android AAPT Problem
Description Resource Path Location Type
error: No resource identifier found for attribute 'adUnitId' in package 'com.google.example'    main.xml    /HelloWorld/res/layout  line 12 Android AAPT Problem
Run Code Online (Sandbox Code Playgroud)

我编辑了main.xml,添加attrs.xml文件,但编译器不喜欢它.

android admob

144
推荐指数
7
解决办法
23万
查看次数

Android - 具有自定义属性的自定义UI

我知道可以创建自定义UI元素(通过View或特定的UI元素扩展).但是有可能为新创建的UI元素定义新的属性或属性(我的意思是不继承,但是全新定义一些我无法用默认属性或属性处理的特定行为)

例如,我的自定义元素:

<com.tryout.myCustomElement
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Element..."
   android:myCustomValue=<someValue>
/>
Run Code Online (Sandbox Code Playgroud)

那么可以定义MyCustomValue吗?

谢谢

android custom-controls

111
推荐指数
2
解决办法
6万
查看次数

在Android中声明可设置的属性

关于declare-styleable标记的珍贵文档很少,我们可以通过它来声明组件的自定义样式.我确实找到了这个标签format属性的有效值列表attr.虽然这很好,但它没有解释如何使用其中的一些值.浏览attr.xml(标准属性的Android源代码),我发现你可以做以下事情:

<!-- The most prominent text color.  -->
<attr name="textColorPrimary" format="reference|color" />
Run Code Online (Sandbox Code Playgroud)

format属性显然可以设置为值的组合.据推测,该format属性有助于解析器解释实际的样式值.然后我在attr.xml中发现了这个:

<!-- Default text typeface. -->
<attr name="typeface">
    <enum name="normal" value="0" />
    <enum name="sans" value="1" />
    <enum name="serif" value="2" />
    <enum name="monospace" value="3" />
</attr>

<!-- Default text typeface style. -->
<attr name="textStyle">
    <flag name="normal" value="0" />
    <flag name="bold" value="1" />
    <flag name="italic" value="2" />
</attr>
Run Code Online (Sandbox Code Playgroud)

这两个似乎都声明了一组指定样式的允许值.

所以我有两个问题:

  1. 可以采用一组enum值之一的样式属性与可以采用一组值的样式属性之间的区别是什么flag? …

xml android styleable declare-styleable

77
推荐指数
2
解决办法
7万
查看次数

在Android中为主题添加自定义字体

有没有办法在Android的主题中添加自定义字体?

我已阅读快速提示:自定义Android字体,但在这里我们必须以编程方式将自定义字体添加到文本中.

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
txt.setTypeface(font); 
Run Code Online (Sandbox Code Playgroud)

但我想按样式/主题设置自定义字体.

fonts android textview

44
推荐指数
3
解决办法
4万
查看次数

帮助Android库项目中的自定义视图属性

我在Android库项目中有一个自定义的PieTimer视图

package com.mysite.android.library.pietimer;

public class PieTimerView extends View {
...
Run Code Online (Sandbox Code Playgroud)

我还有一个XML属性文件,我在PieTimer中使用它

    TypedArray a = context.obtainStyledAttributes(attrs,
        R.styleable.PieTimerView);
Run Code Online (Sandbox Code Playgroud)

XML样式文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="PieTimerView">
        <attr name="max_size" format="dimension" />
        <attr name="start_angle" format="string" />
        <attr name="start_arc" format="string" />
        <attr name="ok_color" format="color" />
        <attr name="warning_color" format="color" />
        <attr name="critical_color" format="color" />
        <attr name="background_color" format="color" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

我在一个布局文件中有PieTimer用于使用该库的项目,就像这样

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root" android:layout_gravity="center_horizontal"
    xmlns:app="http://schemas.android.com/apk/res/com.mysite.android.library.myapp"
    xmlns:pie="com.mysite.library.pietimer"
    >

<com.mysite.library.pietimer.PieTimerView
    pie:start_angle="270" 
    pie:start_arc="0" 
    pie:max_size="70dp"
    pie:ok_color="#9798AD"
    pie:warning_color="#696DC1"
    pie:critical_color="#E75757"
    pie:background_color="#D3D6FF"

    android:visibility="visible"
    android:layout_height="wrap_content" android:id="@+id/clock"
    android:layout_width="wrap_content" 
    android:layout_alignParentRight="true">
</com.mysite.library.pietimer.PieTimerView>
Run Code Online (Sandbox Code Playgroud)

以前我用的xmlns:app是属性的命名空间, …

android

39
推荐指数
3
解决办法
2万
查看次数

Android中的自定义属性

我正在尝试为所有可编辑元素创建一个名为Tag的自定义属性.我将以下内容添加到attrs.xml中

<declare-styleable name="Spinner">
    <attr name="tag" format="string" />
</declare-styleable>

<declare-styleable name="EditText">
    <attr name="tag" format="string" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

我收到一条错误,说"EditText已经定义了属性标记".是否无法在不同的元素上创建同名的自定义属性?

android custom-attributes

30
推荐指数
2
解决办法
1万
查看次数

使用数组引用作为自定义android视图的XML属性

此问题已解决,请参阅注释以获取详细信息.

我正在扩展现有的Android View并加载一些自定义属性,如使用XML声明自定义Android UI元素定义自定义attrs中所述.

具有布尔和整数格式的属性可以正常工作,但是当我尝试指定对数组资源的引用时,应用程序在启动时崩溃.我在xml资源文件中定义了一个整数数组,我试图将它用作自定义视图的属性.

我可以使用数组资源来设置android Spinner类的"entries"属性而没有错误,所以它似乎是我的实现中的一个问题.logcat消息似乎没有提供有关崩溃的任何具体信息,但我仍然在寻找,所以如果我找到了什么,我会更新.

属性由(在attrs.xml中)声明:

<declare-styleable name="CustomView">
    <attr name="values" format="reference"/>
    <attr name="isActive" format="boolean"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

该数组定义为(在arrays.xml中):

<integer-array name="nums">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</integer-array>
Run Code Online (Sandbox Code Playgroud)

我通过以下方式引用数组:

<com.test.CustomView cv:values="@array/nums" />
Run Code Online (Sandbox Code Playgroud)

这会导致应用程序立即崩溃.此外,如果我引用颜色资源而不是数组,那么应用程序不会崩溃.有人知道如何处理这个问题吗?

android custom-attributes android-custom-view android-xml

29
推荐指数
3
解决办法
1万
查看次数

如何在Android中包含两次布局?

Android的<include />元素允许您包含其他XML布局.对于跨多个活动的公共标题很有用.

但是,如果你想要的东西,包括布局若干倍,在同样的看法?例如,我有一个精心设计的布局,我希望在我的视图中显示三次.每个实例都需要不同的值.由于include基本上是XML并将其粘贴到此处,因此我需要更强大的功能.

有没有一些机制来做到这一点?

(我是否正确解释了自己?)

layout android include

27
推荐指数
2
解决办法
9765
查看次数

Android Drawable:在XML文件中指定形状宽度(以百分比表示)?

我正在尝试创建一个简单的Drawable,我想将其设置为视图的背景(使用setBackgroundDrawable).我只想将drawable的背景划分为2个相等的矩形(50% - 50%),第一个想要用黑色填充,第二个用白色填充:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/back_color_1">
        <shape android:shape="rectangle">
          <solid android:color="#000000" />
        </shape>
    </item>
    <item android:id="@+id/back_color_2">
        <shape android:shape="rectangle">
          <solid android:color="#FFFFFF" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

如何在可绘制XML定义文件中指定每个形状的宽度应为50%?像android:width ="50%"之类的东西.

(我正在使用Android 3.0,但我认为这是一个普通的Android问题.)

PS:您可以在CSS或XAML中执行此操作.

xml android shape drawable

25
推荐指数
2
解决办法
3万
查看次数