Pergunta

I'm new to Android and I'm trying to place a textview in a specific area of the screen using Android. Currently, the textview covers the whole screen. I need it to be restricted to a particular central section. See the screenshot below (I need the textview's frame to be the white square):

The TextViewFrame I Require

Here is the code I am currently using:

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:background="@drawable/help"
    android:gravity="left" 
    android:layout_weight="1">

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:scrollbars = "vertical"
        android:layout_weight="0.60"
        android:textSize="14sp" 
        android:textStyle="bold" 
        android:id="@+id/text"/>

</LinearLayout>

I've tried a few things I can't get it to work, can anyone point out where I'm going wrong?

It seems I should be using LinearLayout in order to set my views to a certain percentage of the screen, but I'm not sure if this is the correct approach?

EDIT: Though the answer I've marked as correct is essentially the right thing to do, if you are going to create specific layouts such as this you will need to create different layout files for each different screen size (generally small, medium, large and x-large). These will be placed in the res folder in your project.

Foi útil?

Solução

You want this , rite ?enter image description here

Here is the code for it :-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/claim_detail_header_bg" >

    <TextView
        android:id="@+id/text"
        android:text="sgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsf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sfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginBottom="40dp"
        android:background="@drawable/ic_launcherwrweq"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

Outras dicas

simply give layout_height and than used layout_gravity for set the textview in center horizontal or center vertical....

try using margins and padding. Basically the margins are the offset of the view from the parent view, the padding is the offset of the view content.

layout_marginTop
layout_marginBottom
layout_marginLeft
layout_marginRight

layout_paddingTop
layout_paddingBottom
layout_paddingLeft
layout_paddingRight
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top