android:layout_height 屏幕大小的 50%

标签 android android-layout size android-linearlayout android-layout-weight

我刚刚在 LinearLayout 中实现了一个 ListView,但我需要定义 LinearLayout 的高度(它必须是屏幕高度的 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);

最佳答案

设置其 layout_height="0dp"*,在其下方添加一个空白 View(或空白 ImageView 或只是一个 FrameLayout ) 的 layout_height 也等于 0dp,并将两个 View 设置为具有 layout_weight="1"

这将在填充屏幕时平均拉伸(stretch)每个 View 。由于两者的权重相同,因此各自占据屏幕的 50%。

*请参阅 adamp 的评论,了解为什么会这样以及其他真正有用的花絮。

关于android:layout_height 屏幕大小的 50%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794141/

相关文章:

css - HTML 和 CSS 页面大小的大小

r - 如何在样本大小不等的 R 中进行单向方差分析?

Android Studio 无需单击按钮即可播放音频文件

android - 从状态栏android中删除阴影

android - 我可以在依赖它的 APK 中使用 Android AAR 的资源吗?

android - 如何在运行时更改 RadioGroup 的背景颜色?

android - 公式中的制表符键盘键

ios - iTunes Connect 中的 App Store 文件大小警告

android - 根据gradle风格仅将某些 Assets 复制到APK

android - 添加 ScrollView 时出错