How to make a textview start at 26% of the screen and go until 96% of the screen?
I was wondering how I could make a TextView shows up like the pictures shown below:
small http://raphaeu.com/gvt/img_forum_small.jpg average http://raphaeu.com/gvt/img_forum_medium.jpg big http://raphaeu.com/gvt/img_forum_large.jpg
In other words, I need to be able to position a TextView in the following way:
The top-left corner of the TextView must be at 38% of the screen (vertical) and 26% of the screen (horizontal).
The bottom-right corner of the TextView must be at 100% of the screen (vertical) and 96% of the screen (horizontal).
That way I can guarantee that the TextView layout will be always proportional the way I want, no matter the screen-size of the device.
PS: For a开发者_运维知识库ll means, I’m considering that the coordinate-system on Android starts at the top-left corner of the screen (0, 0) and the coordinates of bottom-right corner of the screen is (100%, 100%).
Thank you very much in advance!
Usually, positioning views in Android is done in XML. Check out this post, it surely will help you: Declaring Layout
You can do this with android:layout_weight parameter. If you have 2 views inside LinearLayout and want the first to be 25% and the second 75% you set their weights to 75 and 25 (ie in the reverse order).
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="70" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="26"
android:text="Text" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="96" />
</LinearLayout>
Later edit: Modified to include scrolling.
It's doable, but it gets very ugly quick. Here's what I managed to do (the view you want is the red rectangle):
I used a TableLayout
and layout_weight
attributes.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:layout_weight="38" />
<TableRow android:layout_weight="62">
<TextView android:layout_weight="26" />
<ScrollView android:layout_weight="70"
android:layout_height="match_parent"
android:layout_width="0dp"
android:background="#ff0000">
<TextView android:layout_width="match_parent"
android:layout_weight="match_parent" />
</ScrollView>
<TextView android:layout_weight="4" />
</TableRow>
</TableLayout>
If you do not mind creating your views programatically, at runtime you can find the size of the screen and then just multiply by whichever constants you need to start the text view at the appropriate spot.
You could also use android:padding
or android:layout_margin
as per
padding or margin
精彩评论