標題:
style
[打印本頁]
作者:
tonyh
時間:
2018-7-21 15:54
標題:
style
本帖最後由 tonyh 於 2018-7-21 17:40 編輯
試著於 styles.xml 新增風格,並於配置元件時直接套用風格設定。在此以 TextView 為例。
[attach]4498[/attach]
layout/activity_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" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:text="@string/hello"
style="@style/Style01"
android:id="@+id/tv1"
android:layout_height="50sp"
android:layout_width="match_parent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50sp"
android:text="@string/hello"
android:id="@+id/tv2"
android:layout_gravity="center_horizontal"
style="@style/Style02" />
</LinearLayout>
複製代碼
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Style01">
<item name="android:background">#e20000</item>
<item name="android:textColor">#fff0f0</item>
<item name="android:textSize">30sp</item>
<item name="android:gravity">center</item>
</style>
<style name="Style02" parent="Style01">
<item name="android:background">#006de2</item>
<item name="android:textColor">#f0f6ff</item>
<item name="android:layout_marginTop">10dp</item>
</style>
</resources>
複製代碼
作者:
李允軒
時間:
2018-7-21 16:56
本帖最後由 tonyh 於 2018-7-21 17:13 編輯
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Style01">
<item name="android:background">#db0000</item>
<item name="android:textSize">30sp</item>
<item name="android:textColor">#ffd0d0</item>
<item name="android:gravity">center</item>
</style>
<style name="Style02" parent="Style01">
<item name="android:background">#0012d7</item>
<item name="android:layout_marginTop">10dp</item>
</style>
</resources>
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<TextView android:text="Hello World!" android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Style01"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
style="@style/Style02" />
</LinearLayout>
複製代碼
作者:
林宇翔
時間:
2018-7-21 17:18
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="style01">
<item name="android:text">@string/str1</item>
<item name="android:background">#d52222</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">30dp</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
<style name="style02">
<item name="android:text">@string/str1</item>
<item name="android:background">#0022ff</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">30dp</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
style="@style/style01" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
style="@style/style02" />
</LinearLayout>
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2