2012-04-24 4 views
0

J'avais créé un ListView personnalisé dans lequel j'ai ajouté deux TextView, un des TextView montre la date et l'autre TextView montre le poids entrent sur cette date particulière. Maintenant, ce que je veux, c'est que dans la deuxième TextView montrant les poids, la largeur de la vue de texte défini comme poids entrer par l'utilisateur. Le code que j'ai écrit montre quelques problèmes. signifie que lorsque l'utilisateur entre le poids 70 et 75 la largeur de TextView montre la même taille. Pourquoi cela arrive-t-il? S'il vous plaît, aidez-moi à résoudre cela.Comment régler la largeur différente dans textView

code XML pour la liste Activité

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/relativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/img_allnotes_bg" > 

<TextView 
    android:id="@+id/txt_weight_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/image_divder1" 
    android:layout_marginTop="15dp" 
    android:gravity="center_horizontal|center_vertical" 
    android:text="@string/txt_weight_title" 
    android:textColor="#5f5f5f" 
    android:textSize="@dimen/font_size_page_text" > 
</TextView> 


<ListView 
    android:id="@+id/weight_list_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/bottom_control_bar" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/image_divider2" 
    android:layout_marginBottom="15dp" 
    android:layout_marginTop="2dp" 
    android:cacheColorHint="#00000000" 
    android:divider="@null" 
    android:dividerHeight="0dp" 
    android:listSelector="#00000000" > 
</ListView> 

code XML pour la liste Voir article

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

<TextView 
    android:id="@+id/txt_weightdate_display" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="60dp" 
    android:layout_marginRight="60dp" 
    android:focusable="false" 
    android:textColor="#5f5f5f" 
    android:textSize="@dimen/font_size_page_text" > 
</TextView> 
<TextView 
    android:id="@+id/txt_weight_display" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txt_weightdate_display" 
    android:layout_marginLeft="60dp" 
    android:layout_marginRight="60dp" 
    android:layout_marginTop="5dp" 
    android:gravity="center" 
    android:focusable="false" 
    android:textColor="#5f5f5f" 
    android:background="#00FFFF" 
    android:textSize="@dimen/font_size_page_text" > 
</TextView> 

du code Java pour générer Liste

public void displayWeights() 
{ 
    m_allWeightsList = (ListView) findViewById(R.id.weight_list_view); 

    int iWeightCount = CycleManager.getSingletonObject().getWeightCount(); 

    m_weightsdateDisplay = new String[iWeightCount]; 

    m_weightDisplay = new String[iWeightCount]; 

    m_enStage = new int[iWeightCount]; 

    int i;  

    for (i=0; i<iWeightCount; i++) 
    { 
     m_weightsdateDisplay[i] = ""; 

     Date dtWeightDate = CycleManager.getSingletonObject().getWeightDate(i); 

     m_enStage[i] = CycleManager.getSingletonObject().getCycleStage(dtWeightDate); 

     m_weightsdateDisplay[i] = formator.format(dtWeightDate.getTime()); 

     m_weightDisplay[i] = ""; 

     m_weightDisplay[i] = CycleManager.getSingletonObject().getWeight(dtWeightDate); 
    } 

    m_adapter = new AllWeightsAdapter(this, m_weightsdateDisplay, m_weightDisplay); 

    m_allWeightsList.setAdapter(m_adapter); 

} 

class AllWeightsAdapter extends BaseAdapter 
{ 
    public String m_weightsDateDisplay[]; 

    public String m_weightsDisplay[]; 

    public Activity m_context; 

    public LayoutInflater m_inflater; 

    public AllWeightsAdapter(Activity m_context, String[] m_weightsDateDisplay, String[] m_weightsDisplay) 
    { 
     super(); 

     this.m_context = m_context; 

     this.m_weightsDateDisplay = m_weightsDateDisplay; 

     this.m_weightsDisplay = m_weightsDisplay; 

     this.m_inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    } 
    public AllWeightsAdapter() 
    { 

    } 
    public int getCount() 
    { 
     return m_weightsDateDisplay.length; 
    } 

    public Object getItem(int position) 
    { 
     return null; 
    } 

    public long getItemId(int position) 
    { 
     return 0; 
    } 

    public class ViewHolder 
    { 
     TextView txt_weightdt_display; 

     TextView txt_weight_display; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ViewHolder holder; 

     Typeface face = Typeface.createFromAsset(m_context.getAssets(), "fonts/Cicle Semi.ttf"); 

     if(convertView == null) 
     { 
      holder = new ViewHolder(); 
      convertView = m_inflater.inflate(R.layout.all_weights_list, null); 

      holder.txt_weightdt_display = (TextView) convertView.findViewById(R.id.txt_weightdate_display); 
      holder.txt_weight_display = (TextView) convertView.findViewById(R.id.txt_weight_display); 
      convertView.setTag(holder); 
     } 
     else 
     { 
      holder = (ViewHolder)convertView.getTag(); 
     } 

     // Added Weight Date in List View Component 
     holder.txt_weightdt_display.setText(m_weightsDateDisplay[position]); 
     holder.txt_weightdt_display.setTypeface(face); 

     // Added Weights in List View Component  
     double factor, width, scale, borderwidth, minScreenWidth = 40, maxScreenWidth = 200; 
     double maxWeight = CycleManager.getSingletonObject().getMaxWeight(); 
     double minWidth = CycleManager.getSingletonObject().getMinWeight(); 

     scale = maxWeight - minWidth; 
     factor = (maxScreenWidth - minScreenWidth)/scale; 

     width = (maxWeight - Double.parseDouble(m_weightsDisplay[position])) * factor; 
     borderwidth = maxScreenWidth - width; 

     holder.txt_weight_display.setText(m_weightsDisplay[position]); 
     holder.txt_weight_display.setMinWidth((int)borderwidth); 
     holder.txt_weight_display.setTypeface(face); 

     return convertView; 
    } 
} 
} 

screnn Tir

enter image description here

+0

Je devine que 70 est> = la taille max Essayez les valeurs qui vont jusqu'à 60, 65. Si oui, alors changez votre largeur à moitié ou quelque chose pour éviter d'atteindre la taille maximale – MikeIsrael

+0

Mais la largeur de la vue est le contenu de l'enveloppe ..Comment résoudre mon problème – AndroidDev

+0

Changez votre échelle pour qu'elle soit plus petite, vous pouvez probablement jouer avec la vue pour donner plus d'espace à cet élément – MikeIsrael

Répondre

0

Les marges gauche/droite qui sont 60dps chacun pourrait être la question. Vous dépensez 120dps pour l'espace vide. http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html dit que la taille de l'écran pour un téléphone serait 320dp largeur.

En outre, je ne suis pas sûr de ce que fait CycleManager, mais si vous ne le faites pas, vous pouvez définir le maximum, vous devez utiliser la largeur de l'écran de l'appareil et l'échelle en conséquence. Enfin, je suppose que ce n'est pas pertinent, mais vous utilisez wrap_content pour le listview. Ne pensez-vous pas qu'il est plus approprié de le faire fill_parent?

0

OK @Anshuman c'est un problème avec realistate d'écran. 70 et 75 sont de la même taille parce que vous avez maximisé l'espace que vous avez pour cette vue. Essayez de changer les proportions de ce marqueur bleu. Peut-être quelque chose comme cela vous donnera plus d'espace (juste jouer avec la variable shrinkScaler jusqu'à ce que vous obtenez la taille dont vous avez besoin pour répondre à tous les poids que vous voulez.

double shrinkScaler = .75; 
holder.txt_weight_display.setMinWidth((int)(borderwidth * shrinkScaler)); 
Questions connexes