2010-09-28 4 views
5

Tôi đã thực hiện tùy chọn tùy chỉnh (nghĩa là tùy chọn có bố cục tùy chỉnh) được hiển thị trong danh sách tùy chọn của số PreferenceActivity.Android: Phong cách sở thích

Bố cục được tạo bằng mã. Vấn đề là phông chữ của TextView được tạo trong mã trông hơi khác so với phông chữ tùy chọn chuẩn của Android.

Vì vậy, giải pháp sẽ là áp dụng thuộc tính kiểu của tùy chọn của android cho số TextView của mình. Các kiểu tương ứng phải là preferenceScreenStyle hoặc preferenceStyle (Tôi không chắc chắn).

Vấn đề của tôi là tôi không thể tìm ra cách đọc các thuộc tính kiểu chuẩn của Android, vì vậy tôi có thể đặt chúng trong mã.

Trả lời

6

Tôi đã cùng một sự cố nhưng tôi đã khắc phục sự cố cho một số thiết bị di động, HTC Saphire và Samsung Galaxy S, nhưng tôi gặp sự cố với HTC Desire HD của mình. Bạn có thể thấy kiểu tùy chọn tiêu chuẩn trong tệp android_SDK_resurces/layout/preferences.xml. Có các lề, cỡ chữ, ...

1

Tôi đã giải quyết vấn đề này bằng cách thay thế tài nguyên bố cục tùy chọn hiện tại bằng tài nguyên bố cục được sử dụng bởi tùy chọn chuẩn như EditTextPreference ví dụ: Đây là một ví dụ mã, lưu ý rằng TimePreference là tùy chọn tùy chỉnh.

TimePreference wake_time = (TimePreference)findPreference("wake_time"); 
    EditTextPreference exercise = (EditTextPreference)findPreference("exercise"); 
    int r = exercise.getLayoutResource(); 
    wake_time.setLayoutResource(r); 
3

Ví dụ về necro nhưng tôi không thể tìm thấy câu trả lời này trên bất kỳ câu hỏi SO nào về kiểu tùy chọn. Cuối cùng tôi đã tìm thấy câu trả lời: tùy chọn mặc định hiện sử dụng layout/preference_material. Bạn có thể xem nó và bố cục cụ thể hơn tại nguồn Android here. Chỉ được sao chép dưới đây trong trường hợp ngắt liên kết:

<?xml version="1.0" encoding="utf-8"?> 
<!-- Copyright (C) 2014 The Android Open Source Project 
    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 
      http://www.apache.org/licenses/LICENSE-2.0 
    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 
<!-- Layout for a Preference in a PreferenceActivity. The 
    Preference is able to place a specific widget for its particular 
    type in the "widget_frame" layout. --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/listPreferredItemHeightSmall" 
    android:gravity="center_vertical" 
    android:paddingStart="?attr/listPreferredItemPaddingStart" 
    android:paddingEnd="?attr/listPreferredItemPaddingEnd" 
    android:background="?attr/activatedBackgroundIndicator" 
    android:clipToPadding="false"> 
    <LinearLayout 
     android:id="@+id/icon_frame" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="-4dp" 
     android:minWidth="60dp" 
     android:gravity="start|center_vertical" 
     android:orientation="horizontal" 
     android:paddingEnd="12dp" 
     android:paddingTop="4dp" 
     android:paddingBottom="4dp"> 
     <com.android.internal.widget.PreferenceImageView 
      android:id="@+id/icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxWidth="48dp" 
      android:maxHeight="48dp" /> 
    </LinearLayout> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:paddingTop="16dp" 
     android:paddingBottom="16dp"> 
     <TextView android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:textAppearance="?attr/textAppearanceListItem" 
      android:ellipsize="marquee" /> 
     <TextView android:id="@+id/summary" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/title" 
      android:layout_alignStart="@id/title" 
      android:textAppearance="?attr/textAppearanceListItemSecondary" 
      android:textColor="?attr/textColorSecondary" 
      android:maxLines="10" 
      android:ellipsize="end" /> 
    </RelativeLayout> 
    <!-- Preference should place its actual preference widget here. --> 
    <LinearLayout android:id="@+id/widget_frame" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="end|center_vertical" 
     android:paddingStart="16dp" 
     android:orientation="vertical" /> 
</LinearLayout>