Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: creating two columns in a linearlayout

<LinearLayout     android:id="@+id/linearLayout1"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="vertical" >      <LinearLayout         android:id="@+id/linearLayout2"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:orientation="horizontal" >          <TextView             android:id="@+id/textView1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Street"              android:layout_gravity="left"/>          <TextView             android:id="@+id/textView2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="456546546"             android:layout_gravity="right" />      </LinearLayout>  </LinearLayout> 

I'm trying to create a layout with two columns, with one textview on the left side and the other on the right side. However, the textviews are still all on the left side.

like image 378
Adam Avatar asked Dec 09 '11 14:12

Adam


People also ask

How do I make columns in Android?

You just put a separate LinearLayout with android:orientation="horizontal" around each pair of buttons. Then the parent LinearLayaout should have android:orientation="vertical" and the weightsum should be in each horizontal LinearLayout.

Which is better LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout. From here: It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.

What is RelativeLayout and LinearLayout?

Android Layout TypesLinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

What is LinearLayout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.


1 Answers

You should use android:layout_weight attribute. Here is an example:

<LinearLayout     android:id="@+id/linearLayout2"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="horizontal" >      <TextView         android:id="@+id/textView1"         android:layout_width="0dp"         android:layout_weight="1"         android:layout_height="wrap_content"         android:text="Street"          android:layout_gravity="left"         android:background="#88FF0000"/>      <TextView         android:id="@+id/textView2"         android:layout_width="0dp"         android:layout_weight="1"         android:layout_height="wrap_content"         android:text="456546546"         android:layout_gravity="right"          android:background="#8800FF00"/>  </LinearLayout> 

enter image description here

like image 136
inazaruk Avatar answered Oct 06 '22 01:10

inazaruk