0

Android에서 내 앱의 설정 페이지를 구현하려고합니다. 나는이 같은 레이아웃 파일 넣으면 그것은 잘 작동 : 프로그램에서Android PreferenceActivity inflate-exception

<?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); 
     } 
} 

그리고 결과는 아래 사진과 같습니다 :

enter image description hereenter image description here

는하지만 실제로 내 설정 페이지가 전체 화면을 차지할 수 원하는, 그래서 나는 (단지 그림의 상승 여력처럼 - 내가 상상)는 "제목 표시 줄"를 추가 혼자서 레이아웃 XML에서, 또한 I "rating our app"함수를 추가하여 RatingBarto 레이아웃을 추가하십시오.

<!-- 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> 

을하지만 프로그램을 실행할 때, 그것은 치명적인 오류 발생 : 그래서 XML은 아래로 변경

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가 전체 화면을 표시하도록하는 방법은 무엇입니까? 모두에게 감사드립니다!

답변

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. 
+0

감사 @Haresh은 또한 당신의 지난 밤 : –

+0

오 Haresh 같은 해결책을 발견, 한 번 더 질문, 당신이 환경 설정 화면에서 RatingBar를 넣어 불가능하다 생각 하는가? 내가 정의한 preferences_header.xml을 넣으면 어떨까요? 감사! –