相关疑难解决方法(0)

如何为不同的标签声明几个具有相同名称的可设置属性?

我希望我的ViewA和ViewB都有"标题"标签.但我不能把它放进去attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ViewA">
        <attr name="title" format="string" />
    </declare-styleable>
    <declare-styleable name="ViewB">
        <attr name="title" format="string" />
        <attr name="max" format="integer" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

因为错误属性"标题"已经定义.另一个问题显示了这个解

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="title" format="string" />
    <declare-styleable name="ViewB">
        <attr name="max" format="integer" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

但在那种情况下,R.styleable.ViewA_titleR.styleable.ViewB_title没有生成.我需要它们使用以下代码从AttributeSet读取属性:

TypedArray a=getContext().obtainStyledAttributes( as, R.styleable.ViewA);
String title = a.getString(R.styleable.ViewA_title);
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

java xml android styleable declare-styleable

23
推荐指数
2
解决办法
7628
查看次数

android主题 - 定义自定义主题中的颜色

我确信有一个简单的答案,但我无法找到它所以我把它扔进stackoverflow ...... ;-)

我将把它作为一个例子.我有一个Android应用程序,用户可以在首选项中选择主题 - 深色或浅色主题.根据所选主题,我必须在我的应用程序中调整20种颜色.所以我希望我可以在主题中定义颜色,然后在我的TextViews等中使用这些如此定义的颜色的名称.但到目前为止,我无法弄清楚如何做到这一点,在这里和那里找不到任何解决方案.我真的不想为这20种颜色中的每种颜色定义一个额外的黑暗和浅色样式,但到目前为止这似乎是我能找到的唯一解决方案.

非常感谢任何提示

马丁:

更新:

在伪语法中,这就是我正在寻找的.可能吗?

<style name="AppTheme.MyDark" parent="android:Theme">
  -?-> titleColor = "#ffffff"
  -?-> introColor = "#ffaaaa"  
</style>

<style name="AppTheme.MyLight" parent="android:Theme.Light">
  -?-> titleColor = "#000000"
  -?-> introColor = "#004444"  
</style>


<TextView
        android:id="@+id/quoteTitle"
        android:textColor=@titleColor
        ...
</TextView>

<TextView
        android:id="@+id/quoteIntro"
        android:textColor=@introColor
        ...
</TextView>
Run Code Online (Sandbox Code Playgroud)

android android-theme

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

如何在选择/焦点上添加阴影到TextView

我的问题是如何在选择TextView时向文本添加阴影或者选择View TextView.例如,我有一个CheckedTextView,它根据选择的类型改变背景.我还制作了一个文本选择器,可以改变不同状态的颜色.现在我想添加一个阴影,例如View被选中.因此它会改变背景颜色,文本颜色并创建阴影.这是我的文字选择器:

<selector 
xmlns:android="http://schemas.android.com/apk/res/android">

<item 
    android:state_focused="true" 
    android:state_pressed="false"       
    android:color="@android:color/white"
    style="@style/DarkShadow"/>

<item 
    android:state_focused="true" 
    android:state_pressed="true"            
    android:color="@android:color/white"
    style="@style/DarkShadow"/>

<item 
    android:state_focused="false" 
    android:state_pressed="true" 
    android:color="@android:color/white"
    style="@style/DarkShadow"/>

<item 
    android:color="@color/primary_text_light_disable_only"/>
Run Code Online (Sandbox Code Playgroud)

和风格:

<style name="DarkShadow">   
    <item name="android:shadowColor">#BB000000</item>
    <item name="android:shadowRadius">2.75</item>
</style>
Run Code Online (Sandbox Code Playgroud)

现在文本被正确突出显示但没有阴影出现.有谁知道如何解决这个问题?

android

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

根据主题设置颜色

我有一种强调色,我在整个布局中使用它.我把它定义为我应用于TextView的样式.

我的应用程序允许用户在黑暗主题和浅色主题之间进行选择.我想根据所选主题调整强调色.

  • 您无法在主题中定义颜色
  • 您无法根据主题定义状态列表颜色
  • 您无法根据主题分隔资源(如版本或屏幕尺寸一样)
  • 你不能从代码更新资源颜色(这不是100%???)

我应该如何根据所选主题控制我的强调色?

开发指南提供了一个使用自定义颜色的主题示例,它接近我想要的.我需要能够在运行时更改颜色.我知道我可以进入我的代码和X使用这种风格的地方我可以把组件磨掉并按照这种方式设置颜色.但我认为我可以通过风格/主题的某种组合来实现这一目标.

谢谢

android

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

自定义属性的可绘制资源

有没有可能在一些自定义属性中从drawable文件夹中获取资源,所以我可以写:

<com.my.custom.View
    android:layout_height="50dp"
    android:layout_width="50dp"
    ...
    my_custom:drawableSomewhere="@drawable/some_image" />
Run Code Online (Sandbox Code Playgroud)

然后在我的自定义视图类中使用drawable简单执行操作?

xml android drawable

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

Android应用程序的日/夜主题

我有一个应用程序,我需要实现日/夜主题.不幸的是,通过使用样式没有简单的方法来制作主题,我需要能够更新:布局背景,按钮选择器,文本颜色,文本大小,图像,图标,动画.

从我看到我有2个选项:

  1. 有不同的xml布局文件为night/day,所以像home_day.xml/ home_night.xml.应用程序中有大约30个屏幕,因此最终将有60 xml布局.关于活动/片段onCreate,基于当前小时我可以setContentView.这会添加更多xml文件,但可以避免在活动中添加更多代码

  2. 只有一个日/夜布局和活动的onCreatefindviewById我想要主题的每个项目,并根据当前日/夜更新他的属性.这可能会为许多视图生成大量额外代码,查找视图和应用属性.

我的目标是2.但我愿意接受你的任何建议.那么,你会选择什么?为什么?

android android-theme

10
推荐指数
4
解决办法
2万
查看次数

如何专业地组织按钮的样式?(SDK 26 +,min SDK 21)

组织专业Android应用程序按钮样式的最佳实践是什么?假设更大的当代应用程序(SDK 26 +,min SDK 21).

这个问题是可以回答的,因为它们的来源Material Design和设置都Android Studio提供了足够的线索和预期专业用途模式的例子.当然,用户不仅限于这种模式,而是遵循它们,使应用程序与源代码一起发挥良好Material Design并提供最佳可维护性.

我找到了几个与按钮造型相关的成分.

  • @style/Widget.AppCompat.Button
  • @style/Widget.AppCompat.Button.Colored
  • @style/TextAppearance.AppCompat.Button
  • @style/TextAppearance.AppCompat.Widget.Button
  • @color/foreground_material_dark
  • ?colorAccent
  • ?textColorPrimary
  • ?android:colorForeground
  • ?textAppearanceButton

可能会有更多.

  • 所有成分如何相关?
  • 它们如何在专业主题中一起使用?

您可以查找来源.但是,即使知道所有细节也无法全面了解预期用途.这个问题要求绘制图片.

android android-layout android-fonts android-studio android-color

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

在数据绑定中找不到属性的setter

我在我的应用程序中使用LiveData,DataBinding和Kotlin.

我为RecyclerView定义了一个绑定适配器,如下所示:

   class MainListBindings {

    private val TAG = "TasksListBindings"

    companion object {

        @JvmStatic
        @SuppressWarnings("unchecked")
        @BindingAdapter("main_items")
        fun setItems(recyclerView: RecyclerView, items: MutableLiveData<List<Homeitem>>? = null) {

            val adapter: RecyclerMainAdapter = recyclerView.adapter as RecyclerMainAdapter
            //prevent use of null list
            items?.let {

                adapter.swapData(items)
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

和我的reyclerView in XML分配给这个bindingAdapter,如下所示:

  <android.support.v7.widget.RecyclerView
      android:id="@+id/main_recycler"
      app:main_items="@{viewmodel.items}"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)

在我的ViewModel中,我使用过

val items = MutableLiveData<List<Homeitem>>()
Run Code Online (Sandbox Code Playgroud)

创建列表.

但我在构建应用程序时遇到此错误:

Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:main_items' with parameter type android.arch.lifecycle.MutableLiveData<java.util.List<project.data.pojo.Homeitem>> on android.support.v7.widget.RecyclerView. file:/home/user/Yara/bazinama_mag/bazinama/app/src/main/res/layout/fragment_main.xml …
Run Code Online (Sandbox Code Playgroud)

data-binding android kotlin android-livedata

9
推荐指数
2
解决办法
6386
查看次数

在布局中的子元素中使用非android命名空间前缀

我试图在一些Android布局中使用自定义属性,但是当我尝试使用除android:子元素之外的命名空间前缀时,我收到了一个错误(来自Eclipse).请注意,当我custom:在文件的root/parent元素中使用名称空间前缀时,它可以正常工作,而不是子元素.

例如,这是一个指定了自定义命名空间的简单布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    custom:my_tag1="whatever">                            <!-- compiles fine -->

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        custom:my_tag2="true"/>                           <!-- generates an error -->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Eclipse给出的错误(仅在第二次尝试使用custom:前缀时)是:

为标记ImageView找到了意外的名称空间前缀"custom".

如果我使用我的根元素ImageView而不是a LinearLayout,则接受前缀.因此,在子元素中使用命名空间前缀似乎只是一个问题.

此外,如果我尝试向xmlns:custom="http://schemas.android.com/apk/res-auto"ImageView 添加另一个属性,它也会抱怨.

如果有帮助,这里是attrs.xml我正在使用的文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="my_tag1" format="string"/>
    <attr name="my_tag2" format="boolean"/>
</resources>
Run Code Online (Sandbox Code Playgroud)

我在网上看到一些东西让我相信我想做的事应该是可能的.例如,在这里接受的答案中,Qberticus在子类中使用前缀"whatever".同样在这里的帖子.

我不明白.使用非android命名空间前缀只是不允许子元素,或者我做错了什么?

xml eclipse android namespaces android-layout

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

Android:如何在Activity类中获取XML的自定义属性

如何在Activity类中获取属性值"required"?

1. values\attrs.xml

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

2. layout\text.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:custom="http://schemas.android.com/apk/res/com.mycompany.test"
    android:baselineAligned="false"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/txtTest"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:inputType="text" 
        custom:required="true" />
Run Code Online (Sandbox Code Playgroud)

xml android

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