2010-08-20 17 views
1

J'essaye de coder un widget de préférences pour définir une valeur avec un SeekBar. Cela inclut un titre, un résumé, un Seekbar et un TextView pour afficher la valeur actuelle. Tous ceux-ci, à l'exception du titre, doivent apparaître dans une deuxième rangée (en tant que préférences standard).Largeur de barre de recherche dans une préférence personnalisée

Je ne peux pas le faire fonctionner. J'ai d'abord essayé avec un RelativeLayout mais plus loin, règles etc. où pas appliqué. Alors j'essaye avec Linear Layout + TableLayout mais Seekbar n'attrape pas le minimumWidth et reste petit.

l'image here

Tout cela se fait par le code. Ici, il est

LinearLayout layoutv = new LinearLayout(getContext()); 
    layoutv.setOrientation(LinearLayout.VERTICAL); 

    //Title 
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 
    TextView view = new TextView(getContext()); 
    view.setId(VIEW_ID_TITLE); 
    view.setText(getTitle()); 
    view.setTextSize(21); 
    view.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL); 
    view.setTextColor(Color.WHITE); 
    view.setLayoutParams(params1); 
    layoutv.addView(view); 

    //TAble Layout to show all the information 
    TableLayout layout = new TableLayout(getContext()); 

    TableLayout.LayoutParams params0 = new TableLayout.LayoutParams(
      TableLayout.LayoutParams.FILL_PARENT, 
      TableLayout.LayoutParams.WRAP_CONTENT); 
    layout.setLayoutParams(params0); 
    layout.setShrinkAllColumns(true); 

    //Row 
    TableRow tr = new TableRow(getContext()); 

    //Summary 
    TextView summary = new TextView(getContext()); 
    summary.setId(VIEW_ID_SUMMARY); 
    summary.setText(getSummary()); 
    summary.setTextSize(14); 
    summary.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL); 
    tr.addView(summary); 

    //SeekBar 
    SeekBar bar = new SeekBar(getContext()); 
    bar.setId(VIEW_ID_BAR); 
    bar.setMax(maximum); 
    bar.setProgress((int)this.oldValue); 
    bar.setMinimumWidth(80); 
    bar.setOnSeekBarChangeListener(this); 
    tr.addView(bar); 

    //Monitor Box 
    this.monitorBox = new TextView(getContext()); 
    this.monitorBox.setId(VIEW_ID_MONITOR); 
    this.monitorBox.setTextSize(12); 
    this.monitorBox.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC); 
    this.monitorBox.setPadding(2, 5, 0, 0); 
    this.monitorBox.setText(Float.toString((float) bar.getProgress()/10.0f)); 
    tr.addView(this.monitorBox);  

    layout.addView(tr); 

    layoutv.setPadding(15, 5, 10, 5); 

    layoutv.addView(layout); 

    layoutv.setId(android.R.id.widget_frame); 

    return layoutv; 

Un grand merci

+0

FWIW, voici un projet qui implémente un widget 'ColorMixer' (en utilisant trois widgets' SeekBar') et un 'ColorPreference' en utilisant le widget' ColorMixer': http : //github.com/commonsguy/cwac-colormixer – CommonsWare

+0

Merci pour votre commentaire, mais ce n'est pas le cas car je rencontre des problèmes avec minimumWidth – theabdabs

Répondre

0

Voici le code pour RelativeLayout. Les deux textes sont présentés dans chevauché dans la même ligne

RelativeLayout layout = new RelativeLayout(getContext()); 

//Title 
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
TextView view = new TextView(getContext()); 
view.setId(VIEW_ID_TITLE); 
view.setText(getTitle()); 
view.setTextSize(21); 
view.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL); 
view.setTextColor(Color.WHITE); 

layout.addView(view, params1); 

//Summary 
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
TextView summary = new TextView(getContext()); 
summary.setId(VIEW_ID_SUMMARY); 
summary.setText(getSummary()); 
summary.setTextSize(14); 
summary.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL); 
params2.addRule(RelativeLayout.BELOW, VIEW_ID_TITLE); 
params2.addRule(RelativeLayout.ALIGN_LEFT, VIEW_ID_TITLE); 
layout.addView(summary, params2); 


layout.setPadding(15, 5, 10, 5); 

layout.setId(android.R.id.widget_frame); 

return layout;