2013-05-11 60 views
0

我有一個應用程序,我正在爲手機和平板設備設計。當我在我的XML-layout中使用重量屬性時,平板電腦上的間距不是預期的效果,因爲它們在平板電腦上拉得太遠。利潤率,手機和平板電腦,多個片段,android

例子 - 在手機,它看起來像這樣

-5dp ------ Frag1 ------ 5dp-

-5dp ------ Frag2 ------ 5dp-

-5dp - ( - T1 - )( - T2 - ) - 5dp-

所以這是很好的,看上去CLE一個與具有1的weight t1和具有2片劑上的weight T2有正在膨脹的第三個片段和它看起來像這樣

--- Frag1 --- | ----- -------------------------- Frag3 --------------------- ----------

--- Frag2 --- | - (Text1 ---------)----------- - (----- Text2 ------------------------) -

現在我知道這是因爲它的T1 = weight 1這是屏幕的33%,T2 = weight 2佔屏幕的66%。 我的問題:有什麼辦法可以以某種方式居中,或者將兩個元素推在一起,這樣他們看起來不會拉長,並且它們之間有很大的空白空間?所以他們看起來像這樣?

--- Frag1 --- | ------------------------------- Frag3 - ------------------------------

--- Frag2 --- | ------ ------ - (Text1 ---------) - (----- Text2 --------------------)--- ------

我試着在XML左右設置layout-margins但它什麼也沒做

我不介意爲平板電腦做一個完全不同的佈局,但是這個東西將會有很多fragments(50-100),並且每個單獨製作雙打將會非常耗時並且非常頭痛。如果有我能做的事情,我會喜歡fragment容器以我想要的方式操縱外觀。在設置邊距等

+0

使用相對佈局 – manjusg 2013-05-11 17:21:29

+0

@manjusg你能對這個問題的答案擴大?你的意思是像碎片容器嗎?我問,因爲我很少使用相對佈局,問題是我的「重量」屬性。我不確定他們是否在相對佈局中工作 – IrishWhiskey 2013-05-11 17:23:22

+0

您需要不同的佈局。嘗試在另一個佈局中將當前的'Linearlayout'(將寬度設置爲'wrap_content')包裝起來(寬度設置爲fill_parent(這就是您當前使用的'LinearLayout',對吧?))並設置在該佈局上使用'android:gravity =「center」'。 – Luksprog 2013-05-11 17:30:36

回答

0

我最終做的解決這個問題是給片段容器的重量值。最終的結果是,在左邊的片段拉伸的一點,由右邊的一個調整

下面是代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/MainLayout" 
android:baselineAligned="false" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white" 
android:orientation="horizontal" > 

<LinearLayout 
    android:id="@+id/container" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical"> 

<LinearLayout 
    android:id="@+id/store_fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/header_fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:scrollbars="vertical" > 
</LinearLayout> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/content_fragment_container" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content"   
    android:layout_weight="2" 
    android:orientation="vertical" 
    android:scrollbars="vertical" > 
</LinearLayout> 

</LinearLayout> 

最終的結果看起來是這樣的用33%/ 66%在屏幕上的比例。

- Frag1 - | ---- -Frag3 ----- |

- Frag2 -

不是我正在尋找的東西,但它工作得相當好。如果其他人有更好的解決方案,我會接受它。謝謝!