// 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.
Android PreferenceActivity inflate-exception
-
30-06-2022 - |
سؤال
I'm trying to implement a settings page for my app on Android. It worked well if I put the layout file like this:
<?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>
In the programs, the codes calling the layout is:
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);
}
}
And the result works like the pic below:
But actually I want my settings page can take up the full screen, so I add a "title bar"(just like the pic upside - I imagined) by myself in the layout xml, and I also add a "ratings our app" function, adding a RatingBarto my layout. So xml is changed to below:
<!-- 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>
But when I ran the program, it generated fatal error:
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
Could anyone please help me out? Moreover, how to make the PreferenceActivity take up the full screen to show? Thanks to all!
المحلول
لا تنتمي إلى StackOverflow