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 center these. But instead of doing them one by one, I was wondering if there is a smart way of doing this. In web application, using css
, we can do this on a div
:
margin-left: auto;
margin-right: auto;
height: xxx;
width: yyy;
Then the div
will be centered on the page
Is there a similar way in when creating android apps?
Span and div are both generic HTML elements that group together related parts of a web page. However, they serve different functions. A div element is used for block-level organization and styling of page elements, whereas a span element is used for inline organization and styling.
The <span> tag is an inline container used to mark up a part of a text, or a part of a document. The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.
You can use both the span and div tags as a container if you want to make a particular part of the web page distinct and style it differently.
In XML, the equivalent of a Div
, is a ViewGroup
.
Example: LinearLayout
, RelativeLayout
etc.
ViewGroups can contain Views and Controls like: TextView
, Button
and EditView
.
More ViewGroup Subclasses
I created an example of what I think you're asking:
I used a LinearLayout
to hold 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:
android:layout_gravity="center|center_vertical|center_horizontal|right|left"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With