我目前正在做一个景观屏幕的特殊xml布局,其中有4个图像,2个LinearLayouts相邻,每个2个图像。我称这些LinearLayouts为linearLayout1和linearLayout2。
linearLayout1用蓝色矩形标记:

linearLayout2用蓝色矩形标记:

如你所见,第一个使用了大约80%的屏幕,而第二个使用了剩下的屏幕。我当然不想要这个,我想每个人都要50%。我不能使用layout_weight,因为它已经在LinearLayouts中使用了(两个图像的定位),而且嵌套的权重对性能不好。
我尝试了许多不同的变体,但我就是不能让这两个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>发布于 2012-05-11 01:45:14
好吧,我看到这里有两个选项。
LinearLayout替换为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>发布于 2015-07-20 15:09:14
您可以为布局设置“权重”,如下所示:
<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%。:)
发布于 2012-05-11 01:32:44
如果你试图避免使用某个特性达到预期目的,你可能应该考虑你的方法是否是不理想的……看起来你是在尝试将项目排列成行和列。尝试使用TableLayout。
https://stackoverflow.com/questions/10539268
复制相似问题