早上好,我有以下片段:
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>
所以问题确实在其他地方。