Android :Style的使用

Style中文应该是式样,可以使用它指定View的外观,程序的主题等。下面是示例:

1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" 3. Android:orientation="vertical" 4. Android:layout_width="fill_parent" 5. Android:layout_height="fill_parent" 6. > 7.<Button 8. Android:background="#ff00ffff" 9. Android:layout_width="fill_parent" 10. Android:layout_height="wrap_content" 11. Android:textColor="#ffff0000" 12. Android:textSize="18sp" 13. Android:text="Hello android" 14. /> 15.</LinearLayout> 本来我们定义一个View对象是以这种方式进行的,现在我们应用Style。

1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" 3. Android:orientation="vertical" 4. Android:layout_width="fill_parent" 5. Android:layout_height="fill_parent" 6. > 7.<Button 8. style="@style/buttonstyle" 9. Android:text="Hello android" 10. /> 11.</LinearLayout> 在res/velues目录下面新建一个style.xml的文件。文件名是任意的,不过必须要以.xml结尾。

1.<?xml version="1.0" encoding="utf-8"?>
2.<resources> 3. <style name="buttonstyle" parent="@Android:style/TextAppearance.Medium"> 4. <item name="Android:background">#ff00ffff</item> 5. <item name="Android:layout_width">fill_parent</item> 6. <item name="Android:layout_height">wrap_content</item> 7. <item name="Android:textColor">#ff00ffff</item> 8. <item name="Android:textSize">18sp</item> 9. </style> 10.</resources>
这两种定义Button的方式实现的效果是一样的。经过比较就可以知道Style'的使用方法。需要注意的是parent 指定了式样的继承。Android预定义了一些式样,通过合理的继承可以减轻我们的工作。上面的方式是继承Android为我们提供的式样,要继承自己定义的式样,方法稍有不同。

1.<?xml version="1.0" encoding="utf-8"?>
2.<resources>
2. <style name="MyButton.buttonstyle"> 3. .............
4. </style> 5.</resources>
它说明buttonstyle继承于MyButton式样,而MyButton是一个自己定义的式样。这种方式是可以递归的。你可以另外定义一个substyle式样让他继承自buttonstyle,那么就是MyButton.buttonstyle.substyle了。

tips:我们为一个View指定了style,那么它会应用这个style。如果我们为一个ViewGroup指定了式样,那么它的Child View是不会继承它的style的。想要让所有的视图都继承一个Style,那就要用到主题Theme了(这句话虽然源自文档,但是强烈质疑这种做法的有效性)。


前面提到了主题Theme这个话题。我们先看看如何将整个Application应用主题。

在AndroidManifest.xml文件中有个application的子节点,可以添加我们的style应用整个程序。

1.<application Android:icon="@drawable/icon"
2. Android:label="@string/app_name" 3. Android:theme="@style/buttonstyle" 4. > 当然一个程序可能有多个Activity,我们可以分别为他们自定主题。下面的列子让我们的程序透明起来。

1.public class StyleTestActivity extends Activity implements OnClickListener{
2. /** Called when the activity is first created. */ 3. Button button ;
4. @Override 5. public void onCreate(Bundle savedInstanceState) { 6. super.onCreate(savedInstanceState); 7. setContentView(R.layout.main);
8. button = (Button)findViewById(R.id.button);
9. button.setOnClickListener(this); 10. }
11. @Override 12. public void onClick(View v) { 13. // TODO Auto-generated method stub 14. Toast toast = Toast.makeText(getApplicationContext(), "hello Android", Toast.LENGTH_SHORT); 15. toast.show();
16. }
17.}
下面内容在res/velus/style.xml中

1.<?xml version="1.0" encoding="utf-8"?>
2.<resources>
3. <color name="transparent_background">#44004400</color> 4. <style name="transparent"> 5. <item name="Android:windowBackground">@color/transparent_background</item> 6. <item name="Android:windowNoTitle">true</item> 7. <item name="Android:windowIsTranslucent">true</item> 8. <item name="Android:windowAnimationStyle">@+android:style/Animation.Translucent</item> 9. </style>
10.</resources>
在AndroidManifest.xml中指定主题:

1.2.<activity Android:name=".StyleTestActivity"
3.
• Android:label="@string/app_name"
• Android:theme="@style/transparent"> • <intent-filter> • <action Android:name="android.intent.action.MAIN" /> • <category Android:name="android.intent.category.LAUNCHER" /> • </intent-filter> • </activity> 布局mian.xml就自己写吧……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值