标签: android-xml

是否可以在xml描述中旋转drawable?

我正在创建一个应用程序,其资源可以重复使用(因为按钮总是相同,但是镜像或旋转).我确实想要使用相同的资源,所以我不必再添加3个与原始资源完全相同但又已旋转的资源.但我也不想将代码与可以在XML中声明的内容混合,或者使用会花费处理时间的矩阵进行转换.

我在XML中声明了一个两个状态按钮.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/and_card_details_button_down_left_onclick" /> <!-- pressed -->
    <item android:drawable="@drawable/and_card_details_button_down_left" /> <!-- default -->
</selector>
Run Code Online (Sandbox Code Playgroud)

我想重复使用drawable,因为它会相同但旋转90º和45º并且我将按钮分配为drawable.

<Button android:id="@+id/Details_Buttons_Top_Left_Button"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/details_menu_large_button" />
Run Code Online (Sandbox Code Playgroud)

我知道我可以用一个RotateDrawable或一个旋转它,Matrix但正如我已经解释过的那样,我不喜欢这种方法.

是否有可能直接在XML上实现这一点,或者您认为最好的方法是什么?放置所有资源但旋转,在代码中旋转它们?

---编辑--- @dmaxi的答案很棒,这是如何将它与项目列表相结合:)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <rotate 
        android:fromDegrees="90"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:drawable="@drawable/and_card_details_button_up_onclick"/>
    </item>

    <item>
        <rotate
        android:fromDegrees="90"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:drawable="@drawable/and_card_details_button_up_onclick"/>
    </item>

</selector>
Run Code Online (Sandbox Code Playgroud)

android drawable android-layout android-xml

99
推荐指数
4
解决办法
8万
查看次数

如何以编程方式获得强调色?

如何以编程方式获取样式中的强调颜色集,如下所示?

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

android android-xml android-styles

82
推荐指数
7
解决办法
3万
查看次数

不同分辨率支持android

编辑问题:

移动分辨率:
我想设计不同的屏幕dpi,如下面的分辨率.
小320x480,
480×800,
540x960,
720x1280(三星S3),
1080×1920(S4,Nexus5,纳克斯5X,摩托G4),
2560 X 1440(的Nexus 6,纳克斯6P,三星边缘)

平板分辨率:
480×800(Micromax的),
600x1024(三星tab2),
800x1280(nexus 7),
1200x1920(新nexus 7),
2048x1536(nexus 9)

我想根据设备显示分辨率使用不同的字体大小.

Q1)best解决这个问题的方法是什么problem

Q2)什么是抛出编码或XML的最佳选择?

Q3)哪个drawable文件夹代表哪个设备分辨率?

Q4)应用程序启动器图标大小为不同的分辨率?

android screen-resolution android-custom-view android-xml android-drawable

78
推荐指数
5
解决办法
9万
查看次数

如何在android XML中连接多个字符串?

问题
我希望能够在Android XML代码中执行类似的操作:

<string name="title">@string/app_name test</string>
<string name="title2">@string/app_name @string/version_name</string>
Run Code Online (Sandbox Code Playgroud)

关于第一行代码编译器显示此错误

找不到与给定名称匹配的资源(在'title'处,值为'@ string/app_name test')

关于第二行代码编译器显示此错误

找不到与给定名称匹配的资源(在'title2'处,值为'@ string/app_name @ string/version_name')

问题
有人知道如何在Android XML中连接多个字符串吗?

基本原理
在许多代码位(在本例中为XML)中复制变量值是不好的做法.

android android-xml

71
推荐指数
1
解决办法
5万
查看次数

android - 使用支持v7在xml中应用selectableItemBackground

即使我的应用程序中包含android支持v7

加入 android:background="?android:attr/selectableItemBackground"

使我的IDE,Eclipse抛出错误(阻止我编译),通知我selectableItemBackground仅适用于最小Api 11及以上.

如何将此属性添加到XML中的背景?

假设从较高的库复制和粘贴不是解决方案

android android-xml android-support-library

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

未知属性android:layout_width,layout_height,id,gravity,layout_gravity,padding

  • 在所有Android标记上获取未知属性错误.

  • 在布局XML中,自动建议不显示所有属性(如layout_width,layout_height,orientation,orientation和所有其他android属性.)

这是一个快照

在此输入图像描述

在此输入图像描述 我为解决这个问题所做的一切.

  • 清洁构建和重建
  • 删除.idea文件
  • 无效的缓存/重新启动..选项
  • 打开省电模式.

SDk是最新的.

在App Level Gridle中

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.abhishek.ondemandservice"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

应用水平Gride.

buildscript {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-xml android-studio android-layoutparams

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

Android EditText中的垂直内容对齐

我有一个多行EditText:

<EditText 
    android:layout_gravity="center"
    android:id="@+id/txtMessage"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblMessage"
    android:layout_width="wrap_content"
    android:width="250dip"
    android:text=""
    android:maxLength="760"
    android:lines="4">
</EditText>
Run Code Online (Sandbox Code Playgroud)

现在内容与中间对齐.我希望将内容与顶部对齐如下:

垂直对齐的文本示例http://img28.imageshack.us/img28/3972/edittext.png

有没有财产可以做到这一点?

layout android android-xml android-edittext layout-gravity

66
推荐指数
2
解决办法
5万
查看次数

如何通过XML Drawable绘制圆圈 - Android?

我试图在android中的XML文件的帮助下绘制圆圈,但是圆圈没有正确显示.下面的代码显示了Oval.如何在XML文件中创建圆圈.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"> 
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="10dip" />

</shape>
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很明显.

android android-layout android-xml

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

虽然RecyclerView没有足够的内容可以滚动,但AppBarLayout中的工具栏是可滚动的

AppBarLayout中的工具栏是否真的可以滚动,尽管带有"appbar_scrolling_view_behavior"的主容器没有足够的内容来真正滚动?

到目前为止我测试的内容:
当我使用NestedScrollView(带有"wrap_content"属性)作为主容器而TextView作为子容器时,AppBarLayout正常工作并且不滚动.

但是,当我使用仅包含少量条目的RecyclerView和"wrap_content"属性(因此无需滚动)时,即使RecyclerView从未收到滚动事件(使用OnScrollChangeListener测试),AppBarLayout中的工具栏也是可滚动的).

这是我的布局代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/ToolbarStyle" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

具有以下效果,工具栏可滚动,虽然没有必要:

我还通过检查所有RecyclerView项目是否可见并使用RecyclerView的setNestedScrollingEnabled()方法找到了解决此问题的方法.
尽管如此,它似乎更像是我想要的错误.任何意见?:d

编辑#1:

对于可能对我当前的解决方案感兴趣的人,我不得不将setNestedScrollingEnabled()逻辑放在Handler的postDelayed()方法中,延迟时间为5毫秒,因为LayoutManager在调用方法时总是返回-1第一个和最后一个项目是否可见.
我在onStart()方法中使用此代码(在我的RecyclerView初始化之后)以及每次发生RecyclerView内容更改后.

final LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        //no items in the RecyclerView
        if (mRecyclerView.getAdapter().getItemCount() == 0)
            mRecyclerView.setNestedScrollingEnabled(false);
        //if the first and the last item is visible
        else if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0
                && layoutManager.findLastCompletelyVisibleItemPosition() == mRecyclerView.getAdapter().getItemCount() …
Run Code Online (Sandbox Code Playgroud)

android android-xml android-coordinatorlayout android-appbarlayout

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

活动布局:片段类:vs android:name属性

我已经阅读了Android开发人员指南中有关Fragments的文档,我发现有时他们会指定要使用Fragment标记属性android:name进行实例化的类,有时他们会使用class: attribute:

<fragment
    android:name="com.example.news.ArticleReaderFragment"
    android:id="@+id/viewer"
    android:layout_weight="2"
    android:layout_width="0dp"
    android:layout_height="match_parent" />

<fragment
    class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
    android:id="@+id/titles" 
    android:layout_weight="1"
    android:layout_width="0px" 
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

安卓:名称类:互换?如果我在Eclipse中使用自动完成功能,它们都会显示相同的文档提示(即该属性提供了要实例化的类名).当要实例化的类具有与java文件名不同的名称时,可能必须使用第二个名称,例如FragmentLayout.java文件中的TitlesFragment?或者我可以使用语法package.fileDOTjava $ Class也使用android:name属性?

我想为Android Java类提供一些XML标签和属性的文档(我在另一个问题中询问过它).

android xml-attribute android-xml android-fragments

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