最小和目标 SDK 为 21 (Lollipop),不使用支持库。
我正在尝试创建一个浮动操作按钮。到目前为止一切顺利,它确实有效(基于其他一些 SO 线程),代码如下:
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="fabStyle" format="reference"/>
</resources>
Run Code Online (Sandbox Code Playgroud)样式.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="floatingActionButtonStyle">@style/FabStyle</item>
</style>
<style name="FabStyle" parent="android:Widget.ImageButton">
<item name="android:background">@drawable/ripple</item>
<item name="android:elevation">4dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)(我希望按钮继承默认的 ImageButton 特性,并且看起来工作正常,这是正确的方法吗?)
视图代码(注意第二个构造函数中自定义样式属性的用法):
public class FloatingActionImageButton extends ImageButton {
public FloatingActionImageButton(Context context) {
this(context, null);
}
public FloatingActionImageButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.floatingActionButtonStyle);
}
public FloatingActionImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public FloatingActionImageButton(Context …Run Code Online (Sandbox Code Playgroud)我正在建立一个图书馆,并希望使其尽可能通用。
所以我想将textColora 的属性设置TextView为使用我的库的应用程序指定为它的primaryColor.
使用android:textColor="?colorPrimary"似乎给了我一种随机颜色,而不是我的测试应用程序中指定的颜色。这可能是因为它试图在库文件中查找该资源 ID,R.java而不是询问应用程序?
那么是否有可能引用库范围之外的颜色呢?我知道我可以通过引入自定义属性来解决此问题,但我想避免该解决方案,因为它要求库的用户更新其应用程序主题,并且它无法开箱即用。
我最近将我的 Android 项目从 eclipse 导入到 Android Studio。我有 3 个 styles.xml 文件:styles.xml、styles.xml (v11) 和 styles.xml (v14)。我不知道那是如何以及何时发生的。
styles.xml (v11) 包括:
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
Run Code Online (Sandbox Code Playgroud)
styles.xml (v14) 包括:
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
Run Code Online (Sandbox Code Playgroud)
我想知道它们为什么在这里以及我是否可以删除 v11 和 v14 xml 文件。
TextView在 Marshmallow 之前的设备上,a 的文本颜色始终为白色。即使我在 XML 中使用黄色或黑色,除了棉花糖之外,它始终是白色的。
在运行时,它将在所有设备上正常工作。以前,当我使用 Eclipse 时,一切正常,但在 Android Studio 中它没有从 XML 中获取属性。
我搜索过的所有问题都说要更改Theme.AppCompat为Theme.AppCompat.Light,但这除了将白色更改为黑色之外没有任何作用。我认为存在风格错误。这是我的样式代码:
<style name="AppBaseTheme" parent="Theme.AppCompat">
<item name="colorPrimary">@color/khaki</item>
<item name="colorPrimaryDark">@color/khaki</item>
<item name="colorAccent">@color/my_yellow</item>
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
Run Code Online (Sandbox Code Playgroud)
这是我的 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="@+id/icon1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="@drawable/sj_icon"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="@+id/icon1"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:gravity="center_vertical"
android:minHeight="34dp"
android:paddingLeft="7dp"
android:text="hello" …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已经得到解答,但我找不到有效的解决方案。
在 android > lolipop (21) 上,他们有什么方法可以改变我的文本选择手柄的颜色(无需上传新的 png 图像)?
我定义了这种风格
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppEditTextStyle" parent="@android:style/Widget.Material.Light.EditText">
<item name="android:background">@null</item>
<item name="android:textCursorDrawable">@null</item>
<item name="android:drawableTint">#00ffffff</item>
</style>
<style name="AppTheme" parent="@android:style/Theme.Material.Light.NoActionBar">
<item name="android:editTextStyle">@style/AppEditTextStyle</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我收到:错误:找不到与给定名称匹配的资源:attr 'android:drawableTint'。
但在文档(https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableTint)中明确指出 android:drawableTint 存在:(所以我不明白为什么我错过了?
下面的截图显示了我的问题:
文本选择手柄具有白色背景并覆盖其他 UI 元素。如何让背景透明?
或者更好的问题实际上是:我可以做什么来使背景变白?
这是应用于 的样式EditText:
<style name="TextInputLayout" parent="AppTheme">
<item name="android:background">@color/grey_50</item>
<item name="colorControlNormal">@color/grey_500</item>
<item name="colorControlActivated">@color/grey_900</item>
<item name="colorControlHighlight">@color/grey_900</item>
<item name="backgroundTint">@color/grey_900</item>
<item name="colorAccent">@color/grey_900</item>
<item name="colorPrimary">@color/grey_900</item>
<item name="android:textCursorDrawable">@null</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/grey_500</item>
<item name="colorPrimaryDark">@color/grey_500</item>
<item name="colorAccent">@color/grey_700</item>
<item name="android:colorBackground">@color/grey_50</item>
<item name="android:colorForeground">@color/grey_900</item>
<item name="android:textColorPrimary">@color/grey_900</item>
<item name="android:textColorPrimaryInverse">@color/grey_50</item>
<item name="android:textColorSecondary">@color/grey_900</item>
<item name="android:textColorSecondaryInverse">@color/grey_50</item>
<item name="android:windowBackground">@color/grey_50</item>
<item name="android:textColorHint">@color/grey_700</item>
<item name="android:colorActivatedHighlight">@color/grey_900</item>
<item name="colorControlNormal">@color/grey_700</item>
<item name="colorControlActivated">@color/grey_900</item>
<item name="colorControlHighlight">@color/grey_900</item>
<item name="android:textColorHighlight">@color/grey_500</item>
<item name="android:colorControlNormal" tools:targetApi="lollipop">@color/grey_700</item>
<item name="android:colorControlActivated" tools:targetApi="lollipop">@color/grey_900</item>
<item name="android:colorControlHighlight" tools:targetApi="lollipop">@color/grey_900</item>
<item name="android:stateListAnimator" tools:targetApi="lollipop">@null</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/grey_900</item>
<item …Run Code Online (Sandbox Code Playgroud) 我一直在关注有关如何创建应用程序工具栏的官方文档。教程说,按照说明进行操作后,
“您的应用程序现在有一个基本的操作栏。默认情况下,操作栏仅包含应用程序的名称和溢出菜单。”
正如您从这张图片中看到的,我已经做了一些样式来获得我想要的颜色,并且我添加了一个菜单项,只是为了看看它是否正确显示。稍后我将研究如何修复图标的颜色。
我的问题是,正如您所看到的,应用程序标题没有垂直居中。距离顶部太近了。这让我很恼火,因为据我所知,它应该自动居中,而我不知道为什么我的不是,但我仍然尝试使用样式自己解决这个问题,但绝对没有成功。样式属性似乎对工具栏标题的填充没有任何影响。我可以向整个工具栏添加填充,但这也会影响菜单项,但我不希望它这样做。
我的实现如下所示:
活动_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/main_activity_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/MyToolbarStyle"
app:theme="@style/MyToolbarTheme" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
样式.xml:
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyToolbarStyle" parent="Base.Widget.AppCompat.Toolbar">
<item name="android:elevation">4dp</item>
<item name="android:background">@color/colorPrimary</item>
</style>
<style name="MyToolbarTheme" parent="Base.Widget.AppCompat.Toolbar">
<item name="android:textColorPrimary">#ffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)
理想情况下,如果有人可以解释我哪里出了问题,使我的应用程序标题不垂直居中,那就太好了,但如果没有,有人可以告诉我如何在应用程序标题中添加边距或填充吗工具栏以便我可以尝试自己将其居中?
PS 这不是这个问题的重复- 该问题涉及更复杂的自定义工具栏。我的应该是默认实现。
更新: 根据要求,这是我的 MainActivity.java 文件的代码:
package com.example.samplescanner;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu; …Run Code Online (Sandbox Code Playgroud) 我明白为什么reference在设置默认主题时用作指向默认样式的属性的格式,但是 using与定义颜色属性时reference|color仅使用有何不同?color您已经可以使用@color/xxx它已经是对另一个资源的引用,那么引用是隐式的吗?如果不是,那么其中一个的用例是什么?
我一直在遵循使用以下推荐技术对应用程序的自定义小部件进行主题化的最佳实践。
<!-- Attributes holding default styles -->
<attr name="myWidgetStyle" format="reference" />
<attr name="myOtherWidgetStyle" format="reference" />
<!-- Custom attributes -->
<attr name="indicatorColor" format="color|reference" />
<!-- Assigning attributes to controls -->
<declare-styleable name="MyWidget">
<item name="android:text" />
<item name="indicatorColor" />
</declare-styleable>
<declare-styleable name="MyOtherWidget">
<item name="android:text" />
<item name="indicatorColor" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
<style name="ThemeBase">
<!-- Store default style in the style-reference attributes -->
<item name="myWidgetStyle">@style/MyWidget</item>
<item …Run Code Online (Sandbox Code Playgroud) 我在可绘制资源文件中定义了一个形状,用作 TextView 的背景 (scrim_background.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/colorAccent"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
我的 color.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#fff</color>
<color name="light_text">#dfff</color>
<color name="colorPrimary">@color/primary</color>
<color name="colorPrimaryDark">@color/primary_dark</color>
<color name="colorAccent">@color/accent</color>
<color name="primary">#F44336</color>
<color name="primary_dark">#D32F2F</color>
<color name="primary_light">#FFCDD2</color>
<color name="accent">#607D8B</color>
<color name="accentAlpha">#DD607D8B</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#757575</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#BDBDBD</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
我的文本视图的样式是:
<style name="BeerItemTextStyleTitle" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">@color/light_text</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/scrim_background</item>
<item name="android:gravity">start|center_vertical</item>
<item name="android:paddingStart">@dimen/space_small</item>
<item name="android:paddingEnd">@dimen/space_small</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我希望背景颜色的 colorAccent 透明度为 50%。
就像是:
<color name="accent">#88607D8B</color>
Run Code Online (Sandbox Code Playgroud)
但我不想修复这些值,因为我不断改变配色方案。
我尝试将 alpha 直接添加到我的 TextView 中,例如:
<item name="android:alpha">50</item>
Run Code Online (Sandbox Code Playgroud)
但这是不合适的,因为它也增加了文本颜色的透明度。我希望只有背景颜色是透明的。
我不想以编程方式执行此操作,因为这样我就必须找到所有文本视图并一一更改。
- …
android android-view android-resources android-styles android-color
我通过添加以下属性来使用半透明导航栏,使我的应用程序移动导航菜单看起来像一些 Google 应用程序,其中移动导航栏有点透明,内容将通过它可见。
<item name="android:windowTranslucentNavigation">true</item>
Run Code Online (Sandbox Code Playgroud)
我不希望我的观点受到影响。我只需要在有更多内容在屏幕上滚动时才需要它,比如有滚动活动或在 recyclerview 中。但最后我需要我的活动视图不与它重叠。
有什么建议请...