4

早上好,我有以下片段:

public class NewIntervPhotoFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    if(container == null) {
        return null;
    }
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.newintervphoto, container, false);
}
//rest of code goes here 

xml的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText 
    android:id="@+id/newintervPhotoDescription"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/newintervPhotoDescription"
    android:inputType="textCapSentences|textMultiLine"
    android:lines="10"
    android:gravity="left|top"/>

 <Button 
   android:id="@+id/newintervPhotoItem"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:background="@android:color/transparent"
   android:text="@string/TakePhoto"
   android:textStyle="@style/TextStyle"
   android:layout_below="@id/newintervPhotoDescription"
   android:layout_centerHorizontal="true"/>

</RelativeLayout> 

在styles.xml 中:

<style 
name="TextStyle">
  <item name="android:textSize">20sp</item> 
  <item name="android:textColor">#FFFFFF</item>
</style> 

上面的样式在同一个项目的其他xml文件中使用没有问题。在这种形式中,我得到了致命的异常(logcat 的一部分):

01-01 11:49:32.635: E/AndroidRuntime(780): FATAL EXCEPTION: main
01-01 11:49:32.635: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start   activity ComponentInfo{cz.triinfo.terminal/cz.triinfo.terminal.ui.NewIntervPhotoActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class <unknown>
01-01 11:49:32.635: E/AndroidRuntime(780): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class <unknown>
01-01 11:49:32.635: E/AndroidRuntime(780): Caused by: java.lang.reflect.InvocationTargetException
01-01 11:49:32.635: E/AndroidRuntime(780): Caused by: java.lang.NumberFormatException: unable to parse '@2131165192' as integer

现在,当我用异常替换时android:textStyle="@style/TextStyle"消失android:textSize=20sp android:textColor=FFFFFF了。我的问题是:在第一种情况下我做错了什么,即 xmlandroid:textStyle和更一般地说,是否可以在 Fragments 中使用样式?

提前致谢!styles.xml 放在 res/values 中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style 
    name="TextStyle">
    <item name="android:textSize">20sp</item> 
    <item name="android:textColor">#FFFFFF</item>
  </style>
</resources> 

所以问题确实在其他地方。

4

2 回答 2

4

您需要使用 android:textAppearance 而不是 android:textStyle 来应用样式。

于 2013-04-26T18:56:50.690 回答
1

检查您的 styles.XML 如何与 android 开发网站上的格式匹配:这里。你可能错过了resources标签。

[编辑]:刚刚阅读了您的第一条评论,并认为这里值得一提(如果您没有查看我之前提到的链接):

<resources>
    Required
This must be the root node. No attributes.

添加这个会让你的styles.xml看起来像:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="TextStyle">
    <item name="android:textSize">20sp</item> 
    <item name="android:textColor">#FFFFFF</item>
  </style>
</resources>

试一试然后说这不是一个真正的解决方案怎么样?

于 2013-01-01T12:32:29.733 回答