利用getLayoutParams()方法和setLayoutParams()方法


利用getLayoutParams()方法 和 setLayoutParams()方法 重新 设置 控件的 布局,

1、首先利用getLayoutParams()方法,获取控件的LayoutParams。
     eg:LayoutParamslaParams=(LayoutParams)imageView.getLayoutParams();


2、设置该控件的layoutParams参数
    eg: 

       laParams.height=200;
       laParams.width=100;

3、将修改好的layoutParams设置为该控件的layoutParams.
   eg:imageView.setLayoutParams(laParams);













发布了701 篇原创文章 · 获赞 2 · 访问量 8737
展开阅读全文

layoutparams设置宽度和高度填充父容器无效啊

02-23

在initData()对list集合添加imageview的时候发现我设置的imageView的宽和高无效 谢谢了 帮帮小白的我 布局文件 ``` <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|bottom" android:orientation="horizontal" android:padding="20dp" > <ImageView android:id="@+id/img_choose1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_choose" /> <ImageView android:id="@+id/img_choose2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_unchoose" /> <ImageView android:id="@+id/img_choose3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_unchoose" /> <ImageView android:id="@+id/img_choose4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_unchoose" /> <ImageView android:id="@+id/img_choose5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_unchoose" /> <ImageView android:id="@+id/img_choose6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_unchoose" /> <ImageView android:id="@+id/img_choose7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_unchoose" /> <ImageView android:id="@+id/img_choose8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_unchoose" /> <ImageView android:id="@+id/img_choose9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:src="@drawable/page_unchoose" /> </LinearLayout> </FrameLayout> ``` MainActivity ``` package com.example.viewpager; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; public class MainActivity extends Activity { private List<View> list_image = new ArrayList<View>(); private ViewPager pager; private int img[] = { R.drawable.beauty1, R.drawable.beauty2, R.drawable.beauty3, R.drawable.beauty4, R.drawable.beauty5, R.drawable.beauty6, R.drawable.beauty7, R.drawable.beauty8, R.drawable.beauty9 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = (ViewPager) findViewById(R.id.pager); initData(); MyViewPagerAdapter adapter = new MyViewPagerAdapter(list_image); pager.setAdapter(adapter); } private void initData() { // TODO Auto-generated method stub for (int i : img) { ImageView imageView = new ImageView(this); //这里无效啊 怎么办。。 //想设置图片填充容器的。 LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); imageView.setImageResource(i); imageView.setLayoutParams(params); list_image.add(imageView); } } } ``` MyViewPagerAdapter ``` package com.example.viewpager; import java.util.ArrayList; import java.util.List; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class MyViewPagerAdapter extends PagerAdapter{ private List<View> list_image = new ArrayList<View>(); public MyViewPagerAdapter(List<View> list_image){ this.list_image=list_image; } @Override public int getCount() { // TODO Auto-generated method stub return list_image.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(list_image.get(position)); return list_image.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(list_image.get(position)); } } ``` ![图片说明](https://img-ask.csdn.net/upload/201602/23/1456192784_647590.png) ![图片说明](https://img-ask.csdn.net/upload/201602/23/1456192798_220271.png) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览