2013-03-02 94 views
2

我想改變的LinearLayout的高度在列表視圖中使用我如何可以改變該佈局的高度和寬度,我會在這裏把我的代碼,但沒有改變的LinearLayout如何做到這一點更改android中listview中的baseadapter中linearlayout的高度和寬度運行時?

首頁的高度。 XML

<LinearLayout  
     android:id="@+id/homelistlayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
              > 
      <ListView 
      android:id="@+id/listadd1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       > 
     </ListView> 

</LinearLayout> 

Homelist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainlayout" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="5" 
    android:background="@color/color_advertise" 
    > 
     <LinearLayout android:id="@+id/layoutadvertise" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="3" 
         android:padding="2dp" 
        > 

        <ImageView 
          android:id="@+id/imageadvertise" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:scaleType="fitXY" 
          android:src="@drawable/adv" />  

     </LinearLayout> 

    <LinearLayout android:id="@+id/layoutpromotional" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:padding="1dp" 
        > 

        <TextView 
          android:id="@+id/textpromotional" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Medium Text" 
          android:textColor="@android:color/white" 
          android:textSize="12sp" /> 

    </LinearLayout> 


    <RelativeLayout  android:id="@+id/layoutdistance" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:orientation="horizontal" 
         android:layout_weight="1" 
        > 
        <TextView 
          android:id="@+id/textdistance" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_alignParentLeft="true" 
          android:text="Distance :" 
          android:textColor="@color/color_orange" 
          android:textSize="12sp" /> 

        <TextView 
          android:id="@+id/textkm" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="15km" 
          android:textColor="@color/color_orange" 
          android:layout_toRightOf="@id/textdistance" 
          android:textSize="12sp" /> 

        <ImageView 
          android:id="@+id/imagestipciate" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_alignParentRight="true" 
          android:src="@drawable/stipciate" />  

    </RelativeLayout>  

</LinearLayout> 

AdapterClass.java

public class LazyAdapterHomeAdvertise extends BaseAdapter { 

    private static final String TAG_SHOWTEXT="showtext"; 
    String showtext; 
    private static final String TAG_PRODUCTINFO="product_info"; 
    String productinfo; 

    private static final String TAG_THUMBIMAGE="thumbsrc"; 
    String thumbimage; 

    private static final String TAG_DISTANCE="distance"; 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> result; 
    private static LayoutInflater inflater=null; 
    public GridImageLoader gridimageLoader; 
    int imageWidth; 
    int imageheight; 
    Context context; 
    int width; 
    int height; 


    public LazyAdapterHomeAdvertise(Activity a, ArrayList<HashMap<String, String>> r) { 
     activity = a; 
     result=r; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     gridimageLoader=new GridImageLoader(activity.getApplicationContext()); 

     width = a.getWindowManager().getDefaultDisplay().getWidth(); 
     height=a.getWindowManager().getDefaultDisplay().getHeight(); 
     imageWidth = ((width/2)-5); 
     imageheight=(height*25)/100; 

    } 

    public int getCount() { 
     return result.size(); 
    } 

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

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

    public static class ViewHolder 
    { 
     public TextView textpromotional; 
     public TextView textkm; 
     public ImageView imageadvertise; 

    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 

     LinearLayout main =(LinearLayout)vi.findViewById(R.id.mainlayout); 
     main.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageheight)); 
     ViewHolder holder; 
     if(convertView==null) 
     { 
      vi = inflater.inflate(R.layout.homelist, null); 
      holder=new ViewHolder(); 
      holder.textpromotional=(TextView)vi.findViewById(R.id.textpromotional); 
      holder.textkm=(TextView)vi.findViewById(R.id.textkm); 
      holder.imageadvertise=(ImageView)vi.findViewById(R.id.imageadvertise); 
      vi.setTag(holder); 
     } 
     else 

      holder=(ViewHolder)vi.getTag(); 


     holder.textpromotional.setText(result.get(position).get(TAG_SHOWTEXT)); 
     holder.textkm.setText(result.get(position).get(TAG_DISTANCE)); 
     holder.imageadvertise.setTag(result.get(position).get(TAG_THUMBIMAGE)); 
     gridimageLoader.DisplayImage(result.get(position).get(TAG_THUMBIMAGE), activity, holder.imageadvertise); 
     return vi; 
    } 

    public static float convertDpToPixel(float dp, Context context){ 
     Resources resources = context.getResources(); 
     DisplayMetrics metrics = resources.getDisplayMetrics(); 
     float px = dp * (metrics.densityDpi/160f); 
     return px; 
    } 

} 

回答

0

刪除烏爾XML使用機器人android_weight屬性:layout_height = 「120dp」 烏爾願望

實施例:

  <LinearLayout android:id="@+id/layoutadvertise" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="2dp" 
       > 



    </LinearLayout> 
+0

沒有不工作 – 2013-03-05 05:03:00

+0

在所有情況下刪除android:layout_weight爲android:layout_height =「120dp」賦予你的值,然後它將工作 – 2013-03-05 06:09:16

+0

仍然我得到錯誤chek這個鏈接的錯誤http://pastie.org/6404258 – 2013-03-06 13:31:18

相關問題