效果图
这里以设置界面里的关于手机界面的UI布局添加圆角风格,在设置中,Preference(首选项)是一种常用的元素。
通过分析 Preference 源码发现了默认布局文件为 preference.xml,文件路径位于
frameworks/base/core/res/res/layout/preference.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a Preference in a PreferenceActivity. The
Preference is able to place a specific widget for its particular
type in the "widget_frame" layout. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingEnd="?android:attr/scrollbarSize"
android:background="?android:attr/selectableItemBackground" >
<ImageView
android:id="@+android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dip"
android:layout_marginEnd="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignStart="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="4" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>
从源代码中我们分析可以看到对应 Preference 基本属性 icon、title、summary,最外层布局为 LinearLayout,可以通过修改LinearLayout 的 background 属性来实现,因为 Preference 中提供了修改 layoutid 方法
public void setLayoutResource(int layoutResId) {
mLayoutResId = layoutResId;
}
在 xml 中使用 android:layout="@layout/card_preference"
在 java 中使用 mPreference.setLayoutResource(R.layout.card_preference)
这样我们就不用去修改 preference.xml 源码,copy preference.xml 保证里面的 id 对应即可。
1、新建圆角矩形 drawable 资源文件
top_circle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topLeftRadius="16dp" android:topRightRadius="16dp"/>
<solid android:color="@color/zzz_card_background"/>
</shape>
none_circle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/zzz_card_background"/>
</shape>
bottom_circle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomLeftRadius="16dp" android:bottomRightRadius="16dp"/>
<solid android:color="@color/zzz_card_background"/>
</shape>
对应的color文件
<color name="zzz_card_background">#ffffff</color>
2、新建圆角矩形 layout 布局文件
card_preference_top.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dashboard_tile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:minHeight="@dimen/dashboard_tile_minimum_height"
android:background=&#