How can I customize layout files in Android to view text as a percentage?
I am starting with android development and wondering how to render widgets exactly on my android canvas. Here's a picture. My goal is to fill the widget as a percentage.
For example, FIRST FIRST one widget - The text view must have a width and height of 50%. Next widget - Text view should have a width and height of 50%.
The next half of the screen - the right side should have three text views. Each text view should be 1/3 of the height and the height should be 50%. How can i do this?
At the bottom, I want to place the slider. Here is a screenshot of the UI design.
οΏΌ
Here's my failed attempt.
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tune2wizard.modernartui.MainActivity" >
<TextView
android:id="@+id/textViewLabel"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="top|start"
android:background="#ff0000ff"
android:textColor="#ffffffff"
android:text="Modern Art UI" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/name">
<TextView
android:id="@+id/textFirstFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:textColor="#ffffffff"
android:text=" HELLO FIRST FIRST" />
</LinearLayout>
</RelativeLayout>
source to share
Read the "layout_weight" parameter and you get it. I started with Coursera too, good luck)
- http://developer.android.com/guide/topics/ui/layout/linear.html
- Android layout size
- Linear layout and weight in Android
- What does android: layout_weight mean?
And for borders check "layout_margin" ...
source to share
You should put all your child views in LinearLayout instead RelativeLayout
. Thus, you can use the weight parameter to fit the view as per your specifications.
For example, to force a view to fit half of the screen, you specify its width to 0 and its weight to 1, and the other to the side with width 0 and length 1.
source to share
You can use a nested linearlayout with "weight", for example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/slider">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:background="@android:color/holo_blue_light"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:background="@android:color/holo_purple"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".33"
android:background="@android:color/holo_red_dark"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".33"
android:background="@android:color/white"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".33"
android:background="@android:color/holo_blue_dark"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
source to share
Something like this would do the trick:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#9b59b6"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#95a5a6" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#f39c12" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#9b59b6" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#fffff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e74c3c" />
</LinearLayout>
</LinearLayout>
source to share