2016-04-27 53 views
0

下面是我對android上視圖佈局的定義。有三個組件,一個編輯區,一個按鈕和一個地圖。文本字段和按鈕將放置在位於視圖頂部的嵌套線性佈局中。地圖將顯示在編輯區下方。我的問題是在編輯區寬度。我已經指定layout_weight爲10,editfield和1爲按鈕。但是,該視圖將顯示爲以下屏幕截圖。 editfield的寬度非常小。我希望它是按鈕寬度的10倍。我怎樣才能做到這一點?layout_weight不能按預期工作編輯區域

<?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:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
tools:context="com.lenovo.mds.lenovopoc.MainActivity"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
    <EditText 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     /> 
    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="button"/> 
</LinearLayout> 

<com.amap.api.maps2d.MapView 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="7"/> 

</LinearLayout> 

enter image description here

+0

添加的LinearLayout weightSum至11 –

回答

2

你應該給 「match_parent」 來的LinearLayout。 WeightSum不是強制性的。問題是因爲width =「wrap_content」。如果寬度設置爲「Match_parent。」那麼正確對齊

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
    <EditText 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="7" 
     /> 
    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     android:text="button"/> 
</LinearLayout> 
1

在EditText上的父佈局你必須設置寬度= 「match_parent」

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context="com.lenovo.mds.lenovopoc.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 
     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="10" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="button"/> 
    </LinearLayout> 

    <com.amap.api.maps2d.MapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="7"/> 

    </LinearLayout> 
2

安卓weightSum

定義最大權重和,如果未指定,則通過 加上所有子項的layout_weight來計算總和,這可用於 實例給予一個孩子50%的總可用空間達到 ,使其layout_weight爲0.5,並將weightSum設置爲1.0。

您應該使用android:weightSum="11"

最後

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:weightSum="11" 
     android:orientation="horizontal"> 
     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="10" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="button"/> 
    </LinearLayout> 
2

機器人:weightSum = 「11」 是解決你的問題。

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:weightSum="11" 
     android:orientation="horizontal"> 
     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="10" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="button"/> 
    </LinearLayout> 

    <com.amap.api.maps2d.MapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="7"/> 

    </LinearLayout> 
0

你的問題是線性佈局父,不要設置高度'0dp'。你需要把它放在match_parent。此外,其他內部佈局和按鈕,我建議不要把0DP的高度或寬度

+0

爲什麼你認爲不使用0PD?是否有任何其他解決方案可以根據比率值進行佈局組件? –

+0

有使用相對佈局 –

1

你只需要這個。

Give match_parent to your LinearLayout and android:weightsum="11"

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.lenovo.mds.lenovopoc.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="11"> 

     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="8" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:text="button" /> 
    </LinearLayout> 

    <com.amap.api.maps2d.MapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="7" /> 

</LinearLayout> 

Here is screen shot how it Looks.

enter image description here

1

我認爲它會爲你工作。

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 
     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button"/> 
    </LinearLayout> 

    <com.amap.api.maps2d.MapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="7"/> 

</LinearLayout> 

this is the final output