日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python preference界面设置_偏好设置如何更改Preference的样式

發(fā)布時(shí)間:2023/12/19 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python preference界面设置_偏好设置如何更改Preference的样式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在上一篇Android設(shè)置界面5分鐘搞定--Preferences的使用里講到了如何運(yùn)用PreferenceActivity快速的新建出偏好設(shè)置頁面。如果是內(nèi)部應(yīng)用,這樣丟過去是完全沒有問題的,但我們?nèi)粘S檬沁_(dá)不到產(chǎn)品要求的。本篇我們就來看一下如何更改Preference的樣式。

如何更改Preferences的樣式

在android ui的開發(fā)中,要更改一個(gè)控件的樣式,我們往往從這幾個(gè)方面來做

通過控件屬性,如background,textSize等

通過指定控件的樣式 style

設(shè)置主題theme

本來so easy的事情,但我驚奇的發(fā)現(xiàn)居然無法設(shè)置之前那些常用控件屬性及樣式來搞,只有主題控件能搞定。我們常用的三板斧只有Theme一招有效了。

通過度娘大多講的都是通過自定義來實(shí)現(xiàn)的,自定義除了每種子控件都要重寫,感覺與自己寫個(gè)布局差別不大了。

通過Theme來設(shè)置樣式

在styles.xml里定義樣式

@color/colorPrimary

@color/colorPrimaryDark

@color/colorAccent

20dip

#FFffffff

10dp

@drawable/selector_checkbox

@android:color/black

@null

42.0dip

@style/CustomWindowTitleText

@style/customCheckBox

#222222

@drawable/wbg

@style/CustomWindowTitleBackground

36dp

#ff0000

#00ff00

其中用到的setStyle是繼承了Default.NoTitleBar,主要是為了在其它地方也可以使用,當(dāng)然你也可以把屬性都在寫一起。

在AndroidManifest.xml里指定樣式android:theme

android:theme="@style/setStyle">

看下效果 (請(qǐng)?jiān)徫业呐渖?#xff09;

默認(rèn)的效果

設(shè)置主題后的效果

各位,是不是很容易呢。

那下面放大招了

除了上面這種方式,其實(shí)我們可以通過指定布局來達(dá)到這個(gè)效果

通過布局實(shí)現(xiàn)樣式的更改

通過設(shè)置android:layout ,android:widgetLayout

這2個(gè)屬性通過布局文件來定義視圖。

下面看一個(gè)CheckBoxReference的實(shí)現(xiàn)

新建兩個(gè)布局文件

preference_item.xml ,自由指定樣式及布局吧,但請(qǐng)保持id與系統(tǒng)保持一致(CheckBoxReference對(duì)應(yīng)的路徑為frameworks/base/core/res/res/layout/preference_widget_checkbox.xml ,其它的自己找相應(yīng)的就可以了)。

<?xml version="1.0" encoding="UTF-8"?>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@drawable/selector_item"

android:gravity="center_vertical"

android:minHeight="?android:listPreferredItemHeight"

android:orientation="horizontal" >

android:id="@android:id/icon"

android:layout_width="40dp"

android:layout_height="40dp"

android:layout_gravity="center_vertical"

android:layout_marginLeft="3dp"

android:scaleType="fitStart"

android:src="@drawable/appstore" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="4dp"

android:layout_marginTop="4dp" >

android:id="@android:id/title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:ellipsize="marquee"

android:fadingEdge="horizontal"

android:singleLine="true"

android:text="title"

android:textColor="#4d4d4d"

android:textSize="18.0sp" />

android:id="@android:id/summary"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_marginLeft="15dp"

android:layout_toRightOf="@android:id/title"

android:layout_toLeftOf="@android:id/widget_frame"

android:maxLines="2"

android:text="summary"

android:textColor="#AAAAAA"

android:textSize="14sp" />

android:id="@android:id/widget_frame"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_marginLeft="4dp"

android:layout_centerVertical="true"

android:gravity="center_vertical"

android:orientation="vertical" >

checkbox_preference_widget.xml 用于設(shè)置checkbox

<?xml version="1.0" encoding="UTF-8"?>

android:id="@android:id/checkbox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:button="@drawable/selector_checkbox"

android:clickable="false"

android:focusable="false"/>

給CheckBoxReference指定這兩個(gè)屬性,layout是布局文件(通用的),widgetLayout就是用于指定CheckBox的

android:layout="@layout/preference_item"

android:icon="@mipmap/ic_launcher"

android:key="parent_checkbox_preference"

android:summary="選定后子控件可操作"

android:title="父選擇控件"android:widgetLayout="@layout/checkbox_preference_widget" />

就這兩步,樣式妥妥的了。

網(wǎng)上常用的自定義這里就不講了,需要的可以谷哥。

本篇示例源碼請(qǐng)移步github

總結(jié)

以上是生活随笔為你收集整理的python preference界面设置_偏好设置如何更改Preference的样式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。