Android 在代码中控制View的填充方式和宽度、高度

转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51331694 文章出自:薛瑄的博客

你也可以查看我的其他同类文章,也会让你有一定的收货!

在java代码中加载view,如何控制view的大小和填充方式呢??

LayoutParams 之控制填充方式

问题:

有个LinearLayout,用来动态加载别的view页面,EditText的宽度是由里面的内容决定 ,无法显示为match_parent的效果

这里写图片描述

想要的效果:EditText能以match_parent的width来显示。

这里写图片描述

布局文件:

main 布局:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="#fff"  
    android:orientation="vertical" >  

    <LinearLayout  
        android:id="@+id/layout"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" >  
    </LinearLayout>  

</LinearLayout>  

test.xml 布局:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="#fff"  
    android:orientation="vertical" >  

    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="测试页面" />  

    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_margin="5dip"  
        android:orientation="horizontal" >  

        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="账号:" />  

        <EditText  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content" />  
    </LinearLayout>  

    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_margin="5dip"  
        android:orientation="horizontal" >  

        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="密码:" />  

        <EditText  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content" />  
    </LinearLayout>  

</LinearLayout>  

解决办法:

在动态加载的view时定义一个LayoutParams,在添加view时明确定义这个view的LayoutParams

public class TestActivity extends Activity {  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);  
        View view = getLayoutInflater().inflate(R.layout.test, null);  
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
        layout.addView(view, params);  
    }  
}  

LayoutParams 之控制宽度和高度

 private Button mbtn;
 mbtn = (Button) findViewById(R.id.btn_test);        
 LayoutParams lp;        
 lp=mbtn.getLayoutParams();
 lp.width=100;
 lp.height=200;        
 mbtn.setLayoutParams(lp);

http://blog.csdn.net/darlk/article/details/7578020
http://www.eoeandroid.com/thread-5687-1-1.html

关注我的公众号,轻松了解和学习更多技术
这里写图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值