在LinearLayout中动态设置宽度

时间:2013-11-24 19:10:03

标签: android dynamic android-linearlayout

我使用本教程http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html创建了自定义搜索栏首选项。我想根据用户的屏幕大小动态调整搜索条的宽度。具体来说,这是教程中设置宽度

的代码
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(80, LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(30, LinearLayout.LayoutParams.WRAP_CONTENT);

我希望80和30的值是动态的。使用不同的布局会更好吗?


编辑:找到以下解决方案似乎正朝着正确的方向发展但尚未完善。

DisplayMetrics size = getContext().getResources().getDisplayMetrics();

LinearLayout layout = new LinearLayout(getContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(size.widthPixels - 350, LinearLayout.LayoutParams.WRAP_CONTENT);      
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

这仍然使用硬编码350。

0 个答案:

没有答案