2016-01-23 91 views
4

我已將一個圖標添加到帶有android:圖標的ListPreference,但在帶有棒棒糖或棉花糖的設備上,該圖標放置在可用空間的左側而不是它在前棒棒糖設備上的中心,以及它應該如何。Android偏好圖標不以設備爲中心=>棒棒糖

設備pre-Lollipop(api 18 - JB 4.3),這是應該的!

enter image description here

後棒棒糖(API 23 - MM),不居中圖標。

enter image description here

PreferenceScreen

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 

    <PreferenceCategory android:title="@string/general"> 

     <ListPreference 
      android:defaultValue="@string/pref_languages_default" 
      android:entries="@array/languages" 
      android:entryValues="@array/listLangValues" 
      android:icon="@drawable/translate" 
      android:key="language" 
      android:title="@string/languages" /> 

    </PreferenceCategory> 

</PreferenceScreen> 

translate.xml繪製

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:height="24dp" 
    android:width="24dp" 
    android:viewportWidth="24" 
    android:viewportHeight="24"> 
    <path android:fillColor="#fff" android:pathData="M12.87,15.07L10.33,12.56L10.36,12.53C12.1,10.59 13.34,8.36 14.07,6H17V4H10V2H8V4H1V6H12.17C11.5,7.92 10.44,9.75 9,11.35C8.07,10.32 7.3,9.19 6.69,8H4.69C5.42,9.63 6.42,11.17 7.67,12.56L2.58,17.58L4,19L9,14L12.11,17.11L12.87,15.07M18.5,10H16.5L12,22H14L15.12,19H19.87L21,22H23L18.5,10M15.88,17L17.5,12.67L19.12,17H15.88Z" /> 
</vector> 

我怎樣才能居中上LL或MM?

+0

任何更新?你是否成功解決了這個問題? – filipproch

+0

不,我沒有成功解決這個問題。 –

回答

1

我花了整整一天的時間來弄清楚同樣的事情。我最終將每個首選項的佈局設置爲默認佈局的修改版本。我將圖像視圖填充從-4dp更改爲0dp,將icon_frame的最小寬度從60dp更改爲56dp。

這絕對不是理想的,但它現在可以工作。

所以

添加偏好佈局ListPreference在屏幕偏好:

<ListPreference 
     android:defaultValue="@string/pref_languages_default" 
     android:entries="@array/languages" 
     android:entryValues="@array/listLangValues" 
     android:icon="@drawable/translate" 
     android:key="language" 
     android:title="@string/languages" 
     android:layout="@layout/my_preference"    
     /> 

,打造佈局\ my_preference.xml:

<?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. 
--> 

<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="?android:attr/activatedBackgroundIndicator" 
android:clipToPadding="false" 
android:gravity="center_vertical" 
android:minHeight="?attr/listPreferredItemHeightSmall" 
android:orientation="horizontal" 
android:paddingEnd="?attr/listPreferredItemPaddingRight" 
android:paddingLeft="?attr/listPreferredItemPaddingLeft" 
android:paddingRight="?attr/listPreferredItemPaddingRight" 
android:paddingStart="?attr/listPreferredItemPaddingLeft" 
tools:ignore="NewApi"> 

<android.support.v7.widget.LinearLayoutCompat 
    android:id="@+id/icon_frame" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="0dp" 
    android:layout_marginStart="0dp" 
    android:gravity="start|center_vertical" 
    android:minWidth="56dp" 
    android:orientation="horizontal" 
    android:paddingBottom="4dp" 
    android:paddingEnd="12dp" 
    android:paddingRight="12dp" 
    android:paddingTop="4dp"> 

    <android.support.v7.widget.AppCompatImageView 
     android:id="@+android:id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxHeight="48dp" 
     android:maxWidth="48dp" /> 
</android.support.v7.widget.LinearLayoutCompat> 

<RelativeLayout 
    android:layout_width="0px" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:paddingBottom="16dp" 
    android:paddingTop="16dp"> 

    <android.support.v7.widget.AppCompatTextView 
     android:id="@android:id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?attr/textAppearanceListItem" 
     tools:text="title" /> 

    <android.support.v7.widget.AppCompatTextView 
     android:id="@android:id/summary" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@android:id/title" 
     android:layout_alignStart="@android:id/title" 
     android:layout_below="@android:id/title" 
     android:textAppearance="?android:attr/textAppearanceListItemSecondary" 
     android:textColor="?android:attr/textColorSecondary" 
     tools:text="summary" /> 
</RelativeLayout> 

<!-- Preference should place its actual preference widget here. --> 
<android.support.v7.widget.LinearLayoutCompat 
    android:id="@android:id/widget_frame" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:gravity="end|center_vertical" 
    android:orientation="vertical" 
    android:paddingLeft="16dp" 
    android:paddingStart="16dp" /> 

再次,它絕對不是正確的解決方案,但它是一個解決方案。希望它有幫助。

相關問題