1

I am creating one application and I want my application to open without any issue with view,I tried to use weightsum attribute for that but I do not know why it shows different views all time in different devices,check this..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:background="@drawable/backgroundgd"
 android:weightSum="100.0"
 >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:background="@drawable/another"
    />
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:background="@drawable/secondpart"
    />
 </LinearLayout>

in bluestack it looks like this enter image description here

3 Answers3

1

Change android:layout_width and android:layout_height to "match_parent" of LinearLayout.

<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:background="@drawable/backgroundgd"
 android:weightSum="100.0">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_cntent"
    android:text="@string/hello_world" 
    android:background="@drawable/another"
    />
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:background="@drawable/secondpart"
    />
 </LinearLayout>
Shvet
  • 1,159
  • 1
  • 18
  • 30
1

Give match_parent width and height in container LinearLayout:

<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:background="@drawable/backgroundgd"
>
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:background="@drawable/another"
    />
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:background="@drawable/secondpart"
    />
</LinearLayout>

Also, you are using weight incorrectly.

Weight sum and weight is used to divide screen portion equally between some elements. So if you give weightsum = "3" in container layout, and its 3 children layout mention weight="1", then all three children uses up 33.33% part of container layout. And at that time width and height are kept as 0 dp according to container layout's orientation.

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • what is usee of weight sum and weight –  Dec 11 '14 at 05:18
  • 3
    Weight sum and weight is used to divide screen portion equally between some elements. So if you give weightsum = "3" in container layout, and its 3 children layout mention weight="1", then all three children uses up 33.33% part of container layout. And at that time width and height are kept as 0 dp according to container layout's orientation. – MysticMagicϡ Dec 11 '14 at 05:20
1

Hi Johnson this example may help you.

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="5">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1" />

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3" />
Rathan Kumar
  • 2,567
  • 2
  • 17
  • 24