更改android中listview中baseadapter中linearlayout的高度和宽度运行时间?

时间:2013-03-02 08:48:01

标签: android height width android-linearlayout

我想更改listview中使用的linearlayout的高度如何更改该布局的高度和宽度我将把我的代码放在这里,但不会改变linearlayout的高度如何做到这一点

Home.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;
    }

}

1 个答案:

答案 0 :(得分:-2)

从ur xml中删除android_weight属性使用android:layout_height =“120dp”ur wish

示例:

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



  </LinearLayout>