0

我正在尝试在 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 中添加了一个“标题栏”(就像我想象的图片一样),我还添加了一个“评价我们的应用程序”功能,在我的布局中添加 RatingBar。所以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 占据全屏显示?谢谢大家!

4

1 回答 1

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. 
于 2013-10-15T04:18:53.490 回答