2012-05-10 7 views
6

Tôi hiện đang bố trí xml đặc biệt của màn hình ngang, trong đó có 4 hình ảnh và 2 LinearLayouts nằm cạnh nhau với 2 hình ảnh. Những LinearLayouts này tôi gọi linearLayout1linearLayout2.Tạo hai LinearLayouts có 50% màn hình mà không sử dụng layout_weight

linearLayout1 được đánh dấu bằng hình chữ nhật màu xanh:

linearLayout1

linearLayout2 được đánh dấu bằng hình chữ nhật màu xanh:

linearLayout2

Như bạn có thể thấy, một trong những đầu tiên sử dụng ~ 80% màn hình, trong khi thứ hai sử dụng những gì còn lại. Tôi không muốn điều này tất nhiên, tôi muốn 50% cho mỗi người. Tôi không thể sử dụng layout_weight vì nó đã được sử dụng trong bản thân LinearLayouts (định vị của hai hình ảnh) và trọng số lồng nhau không tốt cho hiệu suất.

Tôi đã thử nhiều biến thể khác nhau, nhưng tôi chỉ đơn giản là không thể có hai LinearLayouts có 50% màn hình mỗi biến thể. Dưới đây là các mã:

<?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> 
+0

Tôi sẽ sử dụng layout_weight lồng nhau. hiệu suất sẽ không tệ như vậy. –

Trả lời

21

Vâng, có hai tùy chọn tôi xem có sẵn ở đây.

  1. Trục vít cảnh báo LINT và sử dụng trọng lượng lồng nhau. Điện thoại nhanh và nó sẽ tạo ra một phần nghìn giây giá trị của sự khác biệt vì bạn chỉ tăng cường bố cục một lần (phần lớn thời gian). Việc bố trí lồng nhau chỉ xấu cho hiệu suất vì inflator cần tạo nhiều lượt truy cập hơn để đo bố cục.

  2. Swap hàng đầu của bạn LinearLayout với một RelativeLayout và sắp xếp hai đứa trẻ đến một vô hình View ở trung tâm như vậy:

<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> 
+0

+1 cho nhận xét cảnh báo LINT (Tôi đã cảm thấy như vậy trong một thời gian dài). Và một giải pháp hợp lý – Barak

+0

Bạn đang nói về "linearlayout hàng đầu" của tôi, bạn có ý nghĩa gì? Bố trí hàng đầu của tôi là relativelayout. – eightx2

+0

Tôi nghĩ bạn đã yêu cầu cách chia bố cục trong 'LinearLayout' mà không sử dụng trọng số. Vì bạn đã có hai bố trí của bạn trong một 'RelativeLayout', sau đó bạn đã là một phần cách để giải pháp # 2. Tạo một thứ ba, vô hình, View được căn giữa trong 'LinearLayout' cha và căn chỉnh hai cái còn lại với cái kia. – DeeV

0

Nếu bạn đang cố gắng tránh sử dụng một tính năng cho mục đích của nó có lẽ bạn nên xem xét nếu cách tiếp cận của bạn là không lý tưởng ... Nó có vẻ như bạn đang cố gắng để sắp xếp các mục vào hàng và cột. Thử sử dụng TableLayout.

0

Without layout_weight, đưa cả những linearlayouts vào cha mẹ LinearLayout

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

trung tâm chúng bằng cách sử layout_gravity, nếu nội dung của những linearlayouts có cùng kích thước, họ sẽ có cùng kích thước

nếu không, bạn vẫn có thể gặp sự cố

và có thể cần phải đổi kích thước bằng mã

3

Bạn có thể thiết lập "Trọng lượng" cho bố trí, giống như này:

<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> 

Vì vậy, mỗi tuyến tính mất 50% màn hình. :)