Android 在不使用 layout_weight 的情况下,让两个 LinearLayouts 各占屏幕的 50%

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10539268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:06:12  来源:igfitidea点击:

Making two LinearLayouts have 50% of the screen each without using layout_weight

androidandroid-layoutandroid-linearlayout

提问by eightx2

I'm currently doing a special xml layout of a landscape screen, where 4 images are present and 2 LinearLayouts are next to each other with 2 images each. These LinearLayouts I call linearLayout1and linearLayout2.

我目前正在做一个横向屏幕的特殊 xml 布局,其中存在 4 个图像,2 个 LinearLayouts 彼此相邻,每个 2 个图像。我调用这些 LinearLayoutslinearLayout1linearLayout2.

linearLayout1is marked with blue rectangle:

linearLayout1用蓝色矩形标记:

linearLayout1

线性布局1

linearLayout2is marked with blue rectangle:

linearLayout2用蓝色矩形标记:

linearLayout2

线性布局2

As you can see, the first one uses ~80% of the screen, while the second one uses what's left. I don't want this of course, I want 50% for each. I can't use layout_weightbecause it's already used in the LinearLayouts themselves (positioning of the two images) and nested weights are bad for performance.

如您所见,第一个使用了大约 80% 的屏幕,而第二个使用了剩余的部分。我当然不想要这个,我想要每个人的 50%。我无法使用,layout_weight因为它已经在 LinearLayouts 本身中使用(两个图像的定位)并且嵌套权重对性能不利。

I've tried many different variations, but I simply can't get the two LinearLayouts to have 50% of the screen each. Here's the code:

我尝试了许多不同的变体,但我根本无法让两个 LinearLayouts 分别拥有 50% 的屏幕。这是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/db1_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/title_container"
    style="@style/TitleBar" >

    <ImageView
        style="@style/TitleBarLogo"
        android:contentDescription="@string/imgViewDesc"
        android:src="@drawable/title_logo" />

    <ImageView
        style="@style/TitleBarSeparator"
        android:contentDescription="@string/imgViewDesc" />

    <TextView style="@style/TitleBarText" />

    <ImageButton
        style="@style/TitleBarAction"
        android:contentDescription="@string/imgViewDesc"
        android:onClick="onClickAbout"
        android:src="@drawable/title_about" />
</LinearLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title_container"
    android:layout_above="@+id/mobFoxView" >

    <!-- LEFT COLUMN -->

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/mobFoxView"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/linearLayout2"
        android:background="@color/white"
        android:gravity="center"
        android:orientation="vertical"
        android:weightSum="2" >

        <ImageView
            android:id="@+id/imgNews"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:contentDescription="@string/imgViewDesc"
            android:onClick="onClickFeature"
            android:src="@drawable/front_news_1" />

        <ImageView
            android:id="@+id/imgReleases"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:contentDescription="@string/imgViewDesc"
            android:onClick="onClickFeature"
            android:src="@drawable/front_releases_1" />
    </LinearLayout>

    <!-- RIGHT COLUMN -->

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_above="@+id/mobFoxView"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/linearLayout1"
        android:background="@color/white"
        android:gravity="center"
        android:orientation="vertical"
        android:weightSum="2" >

        <ImageView
            android:id="@+id/imgArtists"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:contentDescription="@string/imgViewDesc"
            android:onClick="onClickFeature"
            android:src="@drawable/front_artists_1" />

        <ImageView
            android:id="@+id/imgLabels"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:contentDescription="@string/imgViewDesc"
            android:onClick="onClickFeature"
            android:src="@drawable/front_labels_1" />
    </LinearLayout>
</RelativeLayout>

<com.mobfox.sdk.MobFoxView
    android:id="@+id/mobFoxView"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    mode="test"
    publisherId="@string/mobFoxID" />

</RelativeLayout>

回答by DeeV

Well, there are two options I see available here.

好吧,我看到这里有两个选项可用。

  1. Screw that LINT warning and use the nested weights anyway. Phones are fast and it will make milliseconds worth of a difference since you only inflate layouts once (most of the time). Having nested layouts is only bad for performance because the inflator needs to make more passes to measure the layouts.

  2. Swap your top LinearLayoutwith a RelativeLayoutand align the two children to an invisible Viewin the center like so:

  1. 拧紧那个 LINT 警告并使用嵌套的权重。手机速度很快,它会产生几毫秒的差异,因为你只膨胀一次布局(大部分时间)。嵌套布局只会对性能不利,因为充气器需要进行更多的传递来测量布局。

  2. LinearLayout用 a交换你的顶部RelativeLayout并将两个孩子对齐到View中心的隐形,如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
      android:id="@+id/center_point"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:layout_centerInParent="true"/>

    <LinearLayout
      android:id="@+id/left_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentLeft="true"
      android:layout_alignRight="@+id/center_point">
    </LinearLayout>


    <LinearLayout
      android:id="@+id/right_layout"
      android:orientation="horizontal" //default
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentRight="true"
      android:layout_alignLeft="@+id/center_point">
    </LinearLayout>

</RelativeLayout>

回答by Vahidpr

You can set "Weight" for the layouts , like this :

您可以为布局设置“权重”,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
    </LinearLayout>

So each linearlyouts take 50% of the screen. :)

所以每个线性输出占据屏幕的 50%。:)

回答by kabuko

If you're trying to avoid using a feature for its intended purpose you probably should consider if your approach is non-ideal... It seems like you're trying to arrange items into rows and columns. Try using a TableLayout.

如果您试图避免将某个功能用于其预期目的,您可能应该考虑您的方法是否不理想......似乎您正在尝试将项目排列成行和列。尝试使用TableLayout.

回答by CQM

Without layout_weight, put both of those linearlayouts into a parent linearlayout

没有 layout_weight,将这两个线性布局放入父线性布局中

<LinearLayout orientation:horizontal>
           <LinearLayout Child 1 />
           <LinearLayout Child 2 />
</LinearLayout>

center them using layout_gravity , if the content of those linearlayouts are the same size, they should be the same size

使用 layout_gravity 将它们居中,如果这些线性布局的内容大小相同,则它们应该大小相同

if not, then you could still have a problem

如果没有,那么你仍然可能有问题

and may need to resize using code

并且可能需要使用代码调整大小