首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在不使用layout_weight的情况下使两个LinearLayouts各占50%的屏幕

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

Stack Overflow用户
提问于 2012-05-11 01:28:15
回答 4查看 22.9K关注 0票数 9

我目前正在做一个景观屏幕的特殊xml布局,其中有4个图像,2个LinearLayouts相邻,每个2个图像。我称这些LinearLayouts为linearLayout1linearLayout2

linearLayout1用蓝色矩形标记:

linearLayout2用蓝色矩形标记:

如你所见,第一个使用了大约80%的屏幕,而第二个使用了剩下的屏幕。我当然不想要这个,我想每个人都要50%。我不能使用layout_weight,因为它已经在LinearLayouts中使用了(两个图像的定位),而且嵌套的权重对性能不好。

我尝试了许多不同的变体,但我就是不能让这两个LinearLayouts各有50%的屏幕。代码如下:

代码语言:javascript
复制
<?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>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-05-11 01:45:14

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

  1. 拧紧那个线头警告,并使用嵌套权重。手机速度很快,这将使毫秒的差异,因为你只膨胀布局一次(大多数情况下)。使用嵌套布局只会对性能造成负面影响,因为充气需要进行更多的遍历来测量布局。
  2. 将顶部的LinearLayout替换为RelativeLayout,并将两个子元素与中心的不可见View对齐,如下所示:

代码语言:javascript
复制
<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>
票数 26
EN

Stack Overflow用户

发布于 2015-07-20 15:09:14

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

代码语言:javascript
复制
<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>

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

票数 9
EN

Stack Overflow用户

发布于 2012-05-11 01:32:44

如果你试图避免使用某个特性达到预期目的,你可能应该考虑你的方法是否是不理想的……看起来你是在尝试将项目排列成行和列。尝试使用TableLayout

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10539268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档