Container like div, span etc.

Is it possible to create a container like a div? The problem is that I have 4 controls (TextView, Buttons and EditView), and I want to focus them. But instead of doing them one by one, I was wondering if there was a reasonable way to do this. In a web application, using css, we can do this in a div:

margin-left: auto; margin-right: auto; height: xxx; widht: yyy; 

Then the div will center on the page

Is there a similar way when creating Android apps?

+7
android android-layout
source share
2 answers

In XML, the equivalent of a Div is ViewGroup .

Example: LinearLayout , RelativeLayout , etc.

ViewGroups can contain views and controls, such as: TextView , Button and EditView .

Additional subclasses of ViewGroup

I created an example of what I think you are asking:

I used LinearLayout to store our views and controls.

Then I used android:gravity="center_horizontal" , on our LinearLayout , to center the children.

 <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:gravity="center_horizontal" tools:context=".MainActivity" > <TextView android:layout_width="200dp" android:gravity="center_horizontal" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:layout_width="200dp" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditView android:layout_width="200dp" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LinearLayout> 

Here's what it looks like:

Demo

+8
source share
 android:layout_gravity="center|center_vertical|center_horizontal|right|left" 
+1
source share

All Articles