2011-11-15 12 views
7

प्रोग्रामेटिक रूप से एक लेआउट को प्राथमिकता में सेट करना संभव है?वरीयता लेआउट सेट करना और इसमें विशेषता बदलना

यहाँ मैं क्या है, एक बहुत ही सरल परियोजना है - अवधारणा का सबूत

पसंद गतिविधि:

package com.example; 

import android.os.Bundle; 
import android.preference.PreferenceActivity; 
import android.util.Log; 
import android.view.View; 

public class PreferenceExampleActivity extends PreferenceActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 

     ImageView v = (ImageView) findViewById(R.id.iconka); 

    } 
} 

संसाधन एक्सएमएल:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="settings"> 
    <PreferenceCategory 
     android:title="Category Setting Name" 
     android:order="1" 
     android:key="Main"> 
     <Preference 
      android:order="1" 
      android:title="Setting" 
      android:summary="Setting1" 
      android:layout="@layout/profile_preference_row" 
      android:key="profile" /> 
    </PreferenceCategory> 
</PreferenceScreen> 

पसंद के लिए कस्टम लेआउट :

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/widget_frame" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
    android:paddingRight="?android:attr/scrollbarSize"> 



    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dip" 
     android:layout_marginRight="6dip" 
     android:layout_marginTop="6dip" 
     android:layout_marginBottom="6dip" 
     android:layout_weight="1"> 

     <TextView android:id="@+android:id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" /> 

     <TextView android:id="@+android:id/summary" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@android:id/title" 
      android:layout_alignLeft="@android:id/title" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textColor="?android:attr/textColorSecondary" 
      android:maxLines="4" /> 

    </RelativeLayout> 
    <ImageView 
     android:id="@+id/iconka" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     /> 
</LinearLayout> 

मैं जो चाहता हूं वह गतिविधि से "iconka" ImageView तक पहुंचने और वहां से छवि को बदलने में सक्षम होना है। मैं एपीआई 8 (एंड्रॉइड 2.2)

वर्तमान में "वी" शून्य है और मुझे कोई विचार नहीं है कि ऐसा क्यों है।

एक संकेत की बहुत सराहना की जाएगी!

अद्यतन - समाधान: असल में, मुझे एक कस्टम प्राथमिकता की आवश्यकता है जिसे मैं अपनी आवश्यकताओं के लिए संशोधित कर सकता हूं। यह एक व्यावहारिक गाइड है कि अपनी परियोजना में अपनी खुद की कस्टम वरीयता कैसे बनाएं: Android & Amir - Android Preferences जब कोई लेखक कस्टम वरीयता कक्षा बनाता है तो वह हिस्सा देखें।

+0

इस मधुकोश लिए मान्य नहीं है और इसके बाद के संस्करण है, coz PreferenceFragment दृश्य – gaara87

उत्तर

1

imageView पर निम्नलिखित चर्चाओं पर एक नज़र लेने की कोशिश करें। आपकी समस्या के साथ आपकी मदद कर सकता है।

Android: findViewById of an ImageView (custom adapter)

Android : getting NullPointerException for ImageView imag = (ImageView) findViewById(R.id.image)

+0

मैं आपका उत्तर स्वीकार कर रहा हूं, लेकिन यह मेरी समस्या का समाधान नहीं करता है। मुझे एहसास हुआ कि नोड के दृश्य तक पहुंचने के लिए मुझे वरीयता सक्रियता के ListAdapter को प्राप्त करने या बढ़ाने के लिए किसी भी तरह की आवश्यकता है :(हालांकि मैं अभी भी सोच रहा हूं कि वरीयता का getView() नौकरी कर सकता है। बस मुझे यह पता लगाने की ज़रूरत है कि कौन सा है पैरेंट पैरामीटर। –

+0

मुझे लगता है कि आपको पैरेंट व्यू ग्रुप को दूसरे पैरामीटर के रूप में पास करने की आवश्यकता है। @ ओवरराइड सार्वजनिक देखें ViewView (देखें कन्वर्ट व्यू, व्यू ग्रुप पैरेंट) { कन्वर्टव्यू = this.layout == शून्य? onCreateView (पैरेंट): this.layout; वापसी कनवर्ट करें; } – Bryan

+0

मुझे ब्लॉग की टिप्पणियों में, निम्नलिखित यूआरएल पर यह उदाहरण मिला। http://android-journey.blogspot.com/2010/01/for-almost-any-plication-we-need-to.html – Bryan

1

कोशिश getLayoutResource वरीयता के View पाने के लिए और उसके बाद मिलता है अपने ImageView

+1

में आता है इस विधि किसी पूर्णांक (संसाधन) देता है और यह उपयोगी नहीं है। मेरा मानना ​​है कि मुझे .getView() का उपयोग करने की आवश्यकता है, लेकिन मुझे पता नहीं चल सकता कि मुझे दूसरे पैरामीटर के रूप में क्या जाना चाहिए। अगर मैं इसे पास करता हूं .getListView() मुझे उचित दृश्य नहीं देता है। –

8

खींच बालों के 20 मिनट के बाद मैं इस समस्या का एक सुरुचिपूर्ण समाधान मिल गया। सबसे पहले, वरीयता का विस्तार करें, फिर getView को ओवरराइड करें (देखें कन्वर्ट व्यू, व्यू ग्रुप पैरेंट) विधि। मेरा मामला यह था: मेरे पास ऐप आइकन और 2 टेक्स्ट व्यू (ऐप का नाम और संस्करण) के साथ वरीयता लेआउट था। मैं प्रोग्राम संस्करण में ऐप संस्करण बदलना चाहता हूं। मैं यह कैसे करु? बस नीचे दिए गए एक नज़र डालें: जब getView विधि अधिभावी View v = super.getView(convertView, parent);:

public class AboutUsPreference extends Preference { 

    public AboutUsPreference(Context context) { 
     super(context); 
    } 

    public AboutUsPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public AboutUsPreference(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public View getView(View convertView, ViewGroup parent) { 
     View v = super.getView(convertView, parent); 
     ((TextView)v.findViewById(R.id.textView2)).setText(getAppVersion());   
     return v; 
    } 

    private String getAppVersion(){ 
     PackageInfo pInfo = null; 
     try { 
      pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0); 
     } catch (NameNotFoundException e) { 
      Log.e(getClass().getName(), e.getMessage(), e); 
      return ""; 
     } 

     String version = pInfo.versionName; 
     return getContext().getString(R.string.version, version); 
    } 


} 

समाधान यह है। super.getview कॉल आपके लेआउट व्यू को वापस कर देगा।

और मेरे एक्सएमएल वरीयता लगता है:

<com.audioRec.android.settings.aboutUs.AboutUsPreference 
     android:layout="@layout/about_preference_layout"/> 
+0

यह प्रतिभा थी! धन्यवाद! – Nick

+0

@Alexandru आपने मुझे एक बढ़ते सिर दर्द बचाया। धन्यवाद दोस्त। –

+0

कन्वर्ट व्यू और अभिभावक क्या है? – bgenchel

संबंधित मुद्दे