2012-05-08 24 views
18

Esta es una parte de main_alllatestnews.xmlestablecer mediante programación margen/relleno de TextView en un listview

<LinearLayout 
    android:id="@+id/layout_content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/layout_menu" 
    android:layout_below="@id/layout_title" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 
</LinearLayout> 

Esto está lleno de main_alllatestnewslist.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/layout_temp" 
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:background="@drawable/background_news_list" > 

<ImageView 
    android:id="@+id/image_alllatestnewstitle" 
    android:layout_width="134px" 
    android:layout_height="80px" 
    android:layout_marginBottom="5px" 
    android:layout_marginLeft="10px" 
    android:layout_marginRight="10px" 
    android:layout_marginTop="5px" 
    android:scaleType="centerCrop" /> 

<LinearLayout 
    android:id="@+id/test" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/text_particularlatestnewstitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#000000" 
     android:textSize="25px" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="25px" > 

     <TextView 
      android:id="@+id/text_newsdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#999999" 
      android:textSize="15px" /> 

     <TextView 
      android:id="@+id/text_newscategorytitle" 
      android:layout_width="50px" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_gravity="right" 
      android:gravity="right" 
      android:textColor="#ff0000" 
      android:textSize="15px" /> 
    </RelativeLayout> 
</LinearLayout> 

Esta es una parte de main_alllatestnews.java

LayoutInflater liInflater = (LayoutInflater) this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    linear.addView(liInflater 
      .inflate(R.layout.main_alllatestnewslist, null)); 
lv = (ListView) findViewById(android.R.id.list); 
for (int i = 0; i < webservice.news.size(); i++) { 
    maintitle = (TextView) findViewById(R.id.text_particularlatestnewstitle); 
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)maintitle.getLayoutParams(); 
    layoutParams.setMargins(50, 0, 0, 0);  <-- method one 

    maintitle.setPadding(50, 0, 0, 0);   <-- method two 
    maintitle.setLayoutParams(layoutParams); 

    int margin = 100;       <-- method three 
    ((MarginLayoutParams) maintitle.getLayoutParams()).leftMargin = margin; 
} 
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, 
      R.layout.main_alllatestnewslist, from, to); 
    lv.setAdapter(adapter); 

Como puede ver, usé TRES métodos para establecer el margen/relleno de la vista de texto en una vista de lista pero no fue exitosa.

Estoy utilizando xml textview en lugar de crear uno nuevo.

Quiero configurar manualmente porque obtuve if/else aquí porque no se publicó.

Estoy totalmente fuera de la idea de cómo configurarlo en Java.

por favor me proporcione una idea de que uno viable, gracias

((MarginLayoutParams) maintitle.getLayoutParams()).leftMargin = margin; 
+2

Puede hacer esto en getView del adaptador donde infle el diseño ' –

Respuesta

30

maintitle.setPadding(50, 0, 0, 0); (método 2) debería funcionar.

Creo que el problema es que, a pesar de que hace un montón de cambios en el TextView, en el extremo que tipo de inflar un nuevo diseño de nuevo en:

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, 
      R.layout.main_alllatestnewslist, from, to); 

así, hacer un adaptador personalizado, a continuación, infle y personalice su TextView en el getView().

3

tener cuidado, (int) píxeles! = Dp

public int dp2px(int dp) { 
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); 
} 
+3

¿Qué quiere decir exactamente? Esa pregunta ya fue respondida de la mejor manera, podría ser y es como si estuvieras tratando de poner algo que es totalmente irrelevante y poco claro, lo que no ayuda a nadie. –

+1

Sí, tienes razón. Editado –

3

Prueba el siguiente código, que funciona para mí.

textView.setX (40);

Mi solución es para SDK 14 y superior. No en ListView, sino en RecyclerView.

Cuestiones relacionadas