【问题标题】:Increasing CheckboxPreference title and summary text size and glowing of an preference entry增加 CheckboxPreference 标题和摘要文本大小以及首选项条目的发光
【发布时间】:2013-09-18 05:04:13
【问题描述】:

您好,我正在将消息设置作为首选项。

我正在尝试更改 CheckboxPreference 的 android:title 和 android:summary 文本字体大小。

为此,我正在尝试以下代码

   <PreferenceCategory
    android:key="prefcategory1"
    android:title="GENERAL SETTINGS..." >

    <CheckBoxPreference
        android:defaultValue="false"
        android:enabled="true"
        android:key="checkBoxdelete"
        android:layout="@layout/mylayout"     <!--linking it to mylayout.xml -->
        android:summary="Deletes old messages as limits are reached"
        android:title="Delete old messages" />
  </PreferenceCategory>

mylayout.xml

<TextView android:id="@+android:id/title"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="8dp"
    android:textColor="#FFFFFF"
    android:textSize="30px"
    android:textStyle="bold"/>  

<TextView android:id="@+android:id/summary"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="8dp"
    android:textColor="#FFFFFF"
    android:textSize="20px"
    android:textStyle="bold"/>

通过使用它,文本大小会增加,如下面的屏幕截图所示。但是,我看不到复选框。如何解决这个问题?

【问题讨论】:

    标签: android android-preferences listpreference checkboxpreference


    【解决方案1】:

    为了增加复选框首选项中文本的大小,我使用了自定义类。

    import android.content.Context;
    import android.support.v7.preference.CheckBoxPreference;
    import android.support.v7.preference.PreferenceViewHolder;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    
    @SuppressWarnings("unused")
    public class CustomCheckBoxPreference extends CheckBoxPreference {
    
        private Context mContext;
    
        public CustomCheckBoxPreference(Context context) {
            super(context);
            mContext = context;
    
        }
    
        public CustomCheckBoxPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
    
        }
    
        public CustomCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            mContext = context;
    
        }
    
        public CustomCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            mContext = context;
    
        }
    
        @Override
        public void onBindViewHolder(PreferenceViewHolder holder) {
            super.onBindViewHolder(holder);
            TextView textView = (TextView) holder.findViewById(android.R.id.title);
            textView.setTextSize(14); // add your integer value is sp here
        }
    }
    

    我在首选项布局中添加了这个视图 (R.xml.preferences)

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <CustomCheckBoxPreference
            android:defaultValue="true"
            android:key="preferenceKey"
            android:persistent="true"
            android:title="title"/>
    
    
    </PreferenceScreen>
    

    【讨论】:

      【解决方案2】:

      当有更简单的方法可以做到这一点时,这里的大多数答案都是完全错误的或过于复杂而无法实现。

      仅供未来读者使用 - 您可以在 styles.xml 中更改 textSize/ textColor

         <style name="PreferencesTheme" parent="@android:style/Theme.Black">
                  <item name="android:textSize">34sp</item>
                  <item name="android:textColorSecondary">#000000</item>
                  <item name="android:textColorPrimary">#000000</item>
          </style>
      

      textColorPrimary 将改变 checkBoxPreference 的标题颜色,textColorSecondary 将改变摘要的颜色。

      【讨论】:

      • 这不仅改变了偏好屏幕中的文本
      【解决方案3】:

      您需要像这样将复选框添加到您的自定义布局中:

      <CheckBox
          android:id="@+android:id/checkbox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_centerVertical="true"
          android:focusable="false" />
      

      【讨论】:

        【解决方案4】:

        您的摘要占用了复选框的空间。

        尝试在限制后添加“”

        或减小字体大小

        或者您可以显式设置 textView 的宽度或高度。

        【讨论】:

          猜你喜欢
          • 2011-10-28
          • 2021-11-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多