我正在尝试在Android上为我的应用实施设置页面。如果我把布局文件放在这样的位置,它的效果很好:
<?xml version="1.0" encoding="utf-8"?>
<CheckBoxPreference
android:defaultValue="true"
android:key="autoBack"
android:summaryOff="Let strange calls in"
android:summaryOn="Unlisted calls will be blocked"
android:title="Block Strange Calls" />
<PreferenceCategory android:title="Blocking Options" >
<CheckBoxPreference
android:defaultValue="false"
android:key="smsSilence"
android:summaryOff="Off"
android:summaryOn="On"
android:title="Send Blocked caller message" />
<PreferenceScreen android:title="More Options" >
<CheckBoxPreference
android:defaultValue="true"
android:key="cb21"
android:summaryOff="关闭"
android:summaryOn="开启"
android:title="功能1" />
<CheckBoxPreference
android:defaultValue="true"
android:key="cb22"
android:summaryOff="停用"
android:summaryOn="使用"
android:title="功能2" />
</PreferenceScreen>
</PreferenceCategory>
在程序中,调用布局的代码是:
Intent j = new Intent("android.intent.action.PREFS");
startActivity(j);
public class Prefs extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
}
}
结果如下图所示:
但实际上我希望我的设置页面可以占据全屏,所以我在布局xml中添加了一个“标题栏”(就像图片上部 - 我想象的那样),我还添加了一个“评分我们的应用程序“功能,添加RatingBarto我的布局。所以xml改为:
<!-- language: lang-xml -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="35sp"
android:background="@drawable/titlebg" >
<ImageView
android:id="@+id/titleIc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/titleIc"
android:text="RoboGuard"
android:textColor="#ffffffff"
android:textSize="16sp" />
<ImageButton
android:id="@+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/setting" />
</RelativeLayout>
<CheckBoxPreference
android:defaultValue="true"
android:key="autoBack"
android:summaryOff="Let strange calls in"
android:summaryOn="Unlisted calls will be blocked"
android:title="Block Strange Calls" />
<PreferenceCategory android:title="Blocking Options" >
<CheckBoxPreference
android:defaultValue="false"
android:key="smsSilence"
android:summaryOff="Off"
android:summaryOn="On"
android:title="Send Blocked caller message" />
<PreferenceScreen android:title="More Options" >
<CheckBoxPreference
android:defaultValue="true"
android:key="cb21"
android:summaryOff="off"
android:summaryOn="on"
android:title="" />
<CheckBoxPreference
android:defaultValue="true"
android:key="cb22"
android:summaryOff="stop"
android:summaryOn="stop"
android:title="function2" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Rating the App" >
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="255"
android:numStars="5"
android:progress="255"
android:stepSize="0.5" />
</PreferenceCategory>
但是当我运行该程序时,它产生了致命的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: android.view.InflateException: Binary XML file line #4: Error inflating class RelativeLayout
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: android.view.InflateException: Binary XML file line #36: Error inflating class RatingBar
有人可以帮帮我吗?而且,如何让PreferenceActivity占据全屏显示?谢谢大家!
答案 0 :(得分:1)
// try this
**preferences_header.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="@+id/titleIc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RoboGuard"
android:textColor="#ffffffff"
android:textSize="16sp" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="right"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</LinearLayout>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
**preferences.xml**
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBoxPreference
android:defaultValue="true"
android:key="autoBack"
android:summaryOff="Let strange calls in"
android:summaryOn="Unlisted calls will be blocked"
android:title="Block Strange Calls" />
<PreferenceCategory android:title="Blocking Options" >
<CheckBoxPreference
android:defaultValue="false"
android:key="smsSilence"
android:summaryOff="Off"
android:summaryOn="On"
android:title="Send Blocked caller message" />
<PreferenceScreen android:title="More Options" >
<CheckBoxPreference
android:defaultValue="true"
android:key="cb21"
android:summaryOff="off"
android:summaryOn="on"
android:title="" />
<CheckBoxPreference
android:defaultValue="true"
android:key="cb22"
android:summaryOff="stop"
android:summaryOn="stop"
android:title="function2" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
**MainActivity**
public class MainActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences_header);
addPreferencesFromResource(R.layout.preferences);
}
}
// PreferenceCategory not have RatingBar as child view so you can not declare.