我在主题中定义了一种颜色,如下所示:
attrs.xml
<attr name="defaultTextColor" format="color|reference" />
styles.xml
<style name="BaseLightTheme" parent="@style/Holo.Theme.Light">
<item name="defaultTextColor">@color/primary_text_holo_light</item>
...
</style>
<style name="BaseDarkTheme" parent="@style/Holo.Theme">
<item name="defaultTextColor">@color/primary_text_holo_dark</item>
...
</style>
每当我将其用于文本颜色时,只需使用android:textColor="?defaultTextColor"
即可。我想将此颜色用于视图背景,但这会使应用程序崩溃...为什么?我该如何解决?
这只是崩溃了:
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="?defaultTextColor" />
编辑:添加了logcat
这是我对此错误的logcat(第19行是上述意见视图的一行):
04-18 11:12:05.654: E/ACRA(8006): android.view.InflateException: Binary XML file line #19: Error inflating class <unknown>
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.LayoutInflater._createView(LayoutInflater.java:262)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.LayoutInflater.onCreateView(LayoutInflater.java:470)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.LayoutInflater.createViewFromTag(LayoutInflater.java:316)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.LayoutInflater.rInflate(LayoutInflater.java:604)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:422)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:372)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:367)
04-18 11:12:05.654: E/ACRA(8006): at com.mf.androfit.fragments.WorkoutFragment$CardioViewFragment.onCreateViewHandler(WorkoutFragment.java:865)
04-18 11:12:05.654: E/ACRA(8006): at com.mf.activitylibrary.fragments.BaseFragment.onCreateView(BaseFragment.java:185)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.app._HoloFragment.onCreateView(_HoloFragment.java:124)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:928)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1105)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
04-18 11:12:05.654: E/ACRA(8006): at com.mf.pagermanager.MFragStatePagerAdapter.finishUpdate(MFragStatePagerAdapter.java:170)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.widget.LinearLayout.measureVertical(LinearLayout.java:877)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.widget.LinearLayout.onMeasure(LinearLayout.java:956)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
04-18 11:12:05.654: E/ACRA(8006): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
04-18 11:12:05.654: E/ACRA(8006): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
04-18 11:12:05.654: E/ACRA(8006): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:452)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.widget.LinearLayout.measureVertical(LinearLayout.java:784)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.widget.LinearLayout.onMeasure(LinearLayout.java:956)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.widget.MySlidingPaneLayout.onMeasure(MySlidingPaneLayout.java:523)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:639)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
04-18 11:12:05.654: E/ACRA(8006): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.widget.WindowDecorView.onMeasure(WindowDecorView.java:113)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
04-18 11:12:05.654: E/ACRA(8006): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.widget.LinearLayout.measureVertical(LinearLayout.java:877)
04-18 11:12:05.654: E/ACRA(8006): at org.holoeverywhere.widget.LinearLayout.onMeasure(LinearLayout.java:956)
04-18 11:12:05.654: E/ACRA(8006): at android.view.View.measure(View.java:15696)
04-18 11:12:05.654: E/ACRA(8006): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
04-18 11:12:05.654: E/ACRA(8006): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 11:12:05.654: E/ACRA(8006): at com.android.internal.policy.impl
答案 0 :(得分:0)
我认为您不能简单地通过属性定义View的背景。 为此,您需要创建自定义视图。
您可以找到更多信息here。
一个简单的例子是在src目录中创建一个单独的Java类:
public class SuperView extends View {
//You set all background attributes through your java code.
}
之后,您转到layout.xml并添加自定义视图:
<my.application.SuperView
android:...../>
您可以在xml中为该视图添加一些其他常用属性。