2016-07-05 2 views
2

問題は、直線レイアウト内に2つの線形レイアウトが必要なことです。そして、これら2つの線形レイアウトをandroid:layout_weightプロパティに従って縦に並べることをお勧めします。ここに私のレイアウトは
layout_weightはまったく機能していません

<ScrollView> 
    ..... 
    <LinearLayout> 
    ... 
     <LinearLayout>        //1.Here the Linear Layout         is not getting aligned properly according to the layout_weight 
     ... 
      android:layout_height="0dp" 
      android:layout_weight="3" 
      . 
      . 
      . 
      . 
     </LinearLayout> 

     <LinearLayout>        //2.These two Linear layouts are to be aligned vertically and this second layout is required to use very less space as compared to first layout 
     . . . 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      . 
      . 
      . 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 


です。ここ参照のための完全なXMLコードは次のとおりです。

<ScrollView 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" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:layout_margin="16dp" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:orientation="vertical" 
      android:layout_weight="3" > 

      <EditText 
       android:id="@+id/edittext" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="16dp" 
       android:layout_marginTop="30dp" 
       android:ems="5" 
       android:hint="Name" 
       android:inputType="text" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="16dp" 
       android:text="Toppings" 
       android:textAllCaps="true" /> 

      <CheckBox 
       android:id="@+id/whippedcream_checkbox_id" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="16dp" 
       android:paddingLeft="24dp" 
       android:text="Whipped Cream" 
       android:textSize="16sp" /> 

      <CheckBox 
       android:id="@+id/chocolate_checkbox_id" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="16dp" 
       android:paddingLeft="24dp" 
       android:text="Chocolate" 
       android:textSize="16sp" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="16dp" 
       android:text="Quantity" 
       android:textAllCaps="true" /> 

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

       <Button 
        android:layout_width="48dp" 
        android:layout_height="48dp" 
        android:onClick="decrement" 
        android:text="-" /> 

       <TextView 
        android:id="@+id/quantity_text_view" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="8dp" 
        android:layout_marginRight="8dp" 
        android:text="2" 
        android:textColor="@android:color/black" 
        android:textSize="16sp" /> 

       <Button 
        android:layout_width="48dp" 
        android:layout_height="48dp" 
        android:onClick="increment" 
        android:text="+" /> 

      </LinearLayout> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="16dp" 
       android:text="order summary" 
       android:textAllCaps="true" /> 


      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="16dp" 
       android:onClick="submitOrder" 
       android:text="Order" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="PRICE:" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Coffee: Rs.5 per cup" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Whipped Cream: Rs.1 per cup additional" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Chocolate: Rs.2 per cup additional" /> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 
+0

親レイアウトに重さを与える –

+0

レイアウトのスペースを均等にしたいですか? – Piyush

+0

layout_heightを "match_parent"に設定してみてください –

答えて

2
  1. scrollviewにandroid:fillViewport="true"を追加

android:weightSum="4"メインのLinearLayoutを追加こちら以下の完全なコード

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView 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:fillViewport="true" 
tools:context=".DemoActivity"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="16dp" 
    android:orientation="vertical" 
    android:weightSum="4"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/edittext" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:layout_marginTop="30dp" 
      android:ems="5" 
      android:hint="Name" 
      android:inputType="text" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:text="Toppings" 
      android:textAllCaps="true" /> 

     <CheckBox 
      android:id="@+id/whippedcream_checkbox_id" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:paddingLeft="24dp" 
      android:text="Whipped Cream" 
      android:textSize="16sp" /> 

     <CheckBox 
      android:id="@+id/chocolate_checkbox_id" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:paddingLeft="24dp" 
      android:text="Chocolate" 
      android:textSize="16sp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:text="Quantity" 
      android:textAllCaps="true" /> 

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

      <Button 
       android:layout_width="48dp" 
       android:layout_height="48dp" 
       android:onClick="decrement" 
       android:text="-" /> 

      <TextView 
       android:id="@+id/quantity_text_view" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="8dp" 
       android:layout_marginRight="8dp" 
       android:text="2" 
       android:textColor="@android:color/black" 
       android:textSize="16sp" /> 

      <Button 
       android:layout_width="48dp" 
       android:layout_height="48dp" 
       android:onClick="increment" 
       android:text="+" /> 

     </LinearLayout> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="order summary" 
      android:textAllCaps="true" /> 


     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:onClick="submitOrder" 
      android:text="Order" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="PRICE:" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Coffee: Rs.5 per cup" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Whipped Cream: Rs.1 per cup additional" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Chocolate: Rs.2 per cup additional" /> 
    </LinearLayout> 
</LinearLayout> 

+0

こちらは@Vijay、ありがとうございます –

+0

@AakashBansalようこそ –

1

リニアレイアウトの両方の内部のすべてのコンテンツは、wrap_contentとしてのlayout_heightセットを持っています。

ここで重要なことは、layout_height/layout_widthに重み付けされていないすべてのビューが画面上のスペースを占有した後にのみ重みが機能することです。

例:abcd2ここ

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:text="abcd1" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="abcd2" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="abcd3" /> 


</LinearLayout> 

とABCD3 TextViewsは、最初のスペースをallotedされるだろうし、その後ABCD1は、画面上のスペースの残りの部分を占めるだろう。

関連する問題