布局Android TextView相对于屏幕

时间:2013-10-29 13:02:22

标签: android xml textview android-linearlayout

我是Android新手,我尝试使用Android在屏幕的特定区域放置文本视图。目前,textview涵盖了整个屏幕。我需要将它限制在特定的中心部分。请参阅下面的屏幕截图(我需要将textview&#39帧设为白色方块):

The TextViewFrame I Require

以下是我目前使用的代码:

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

我尝试了一些我无法解决问题的事情,有人能指出我出错的地方吗?

我似乎应该使用LinearLayout将视图设置为屏幕的某个百分比,但我不确定这是否是正确的方法?

编辑:虽然我已经标记为正确的答案基本上是正确的,但如果您要创建特定的布局,则需要创建不同的布局文件对于每种不同的屏幕尺寸(通常是小,中,大和x大)。这些将放在项目的res文件夹中。

3 个答案:

答案 0 :(得分:1)

只需给出layout_height,然后使用layout_gravity将textview设置为中心水平或垂直中心....

答案 1 :(得分:1)

你想要这个,仪式?enter image description here

以下是代码: -

<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="sgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffd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vdffdfdsgfdsgdfsfddjvjkfdnbvkfdnvkjfdvfdvdffdfd"
        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>

答案 2 :(得分:1)

尝试使用边距和填充。基本上边距是视图与父视图的偏移量,填充是视图内容的偏移量。

layout_marginTop
layout_marginBottom
layout_marginLeft
layout_marginRight

layout_paddingTop
layout_paddingBottom
layout_paddingLeft
layout_paddingRight