您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
83_在android里面使用样式(style)
发布时间:2021-03-26 10:46:47编辑:雪饮阅读()
传统的样式设置
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ff66ff00"
android:text="hello"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这里建立了一个名称为test_style的样式,将刚才的textView的样式全部转过来了。 然后重回activity_main.xml:
<string name="app_name">style</string>
<style name="test_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#ff66ff00</item>
</style>
</resources>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView style="@style/test_style" android:text="hello"/>
<TextView style="@style/test_style" android:text="world"/>
<TextView style="@style/test_style" android:text="beibei"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<string name="app_name">style</string>
<style name="test_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#ff66ff00</item>
</style>
<style name="test_style1" parent="@style/test_style">
<item name="android:textSize">28sp</item>
</style>
</resources>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView style="@style/test_style1" android:text="hello"/>
<TextView style="@style/test_style1" android:text="world"/>
<TextView style="@style/test_style1" android:text="beibei"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView style="@style/test_style1" android:text="hello"/>
<TextView style="@style/test_style1" android:text="world"/>
<TextView style="@style/test_style1" android:text="beibei" android:textSize="35sp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
关键字词:android,style,样式