大家好我只是在LinearLayout中实现了一个ListView,但我需要定义LinearLayout的高度(它必须是屏幕高度的50%)。android:layout_height屏幕大小的50%
<LinearLayout
android:id="@+id/widget34"
android:layout_width="300px"
android:layout_height="235px"
android:orientation="vertical"
android:layout_below="@+id/tv_scanning_for"
android:layout_centerHorizontal="true">
<ListView
android:id="@+id/lv_events"
android:textSize="18sp"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_scanning_for"
android:layout_centerHorizontal="true">
</ListView>
</LinearLayout>
那可能吗?
我为按钮和EditText做了类似的事情,但似乎不适用于布局。
这是我的代码:
//capture the size of the devices screen
Display display = getWindowManager().getDefaultDisplay();
double width = display.getWidth();
//my EditText will be smaller than full screen (80%)
double doubleSize = (width/5)*4;
int editTextSize = (int) doubleSize;
//define the EditText
userName = (EditText) this.findViewById(R.id.userName);
password = (EditText) this.findViewById(R.id.password);
//set the size
userName.setWidth(editTextSize);
password.setWidth(editTextSize);
谢谢! :)
您是否尝试过在LinearLayout周围使用RelativeLayout并设置RelativeLayout的大小? –
我可以在RelativeLayout高度参数中输入百分比吗? –
使用自定义LayoutParams:http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html与RelativeLayout.setLayoutParams():http://developer.android.com/reference/android/view/View的.html#setLayoutParams%28android.view.ViewGroup.LayoutParams%29。设置高度为display.getHeight()/ 2 –