Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 511
AomSet Avatar asked Sep 12 '13 10:09

AomSet


People also ask

Should I use span or div?

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.

What is div and SPAN tag HTML?

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.

Can we use span inside div?

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.


2 Answers

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:

Demo

like image 122
Evan Bashir Avatar answered Sep 17 '22 15:09

Evan Bashir


android:layout_gravity="center|center_vertical|center_horizontal|right|left"
like image 34
Autocrab Avatar answered Sep 18 '22 15:09

Autocrab