Android TextView相对于ImageView的位置

时间:2013-01-08 15:36:19

标签: android image textview position relative

在我的应用程序中,我有一个ImageView作为活动的背景。此视图填充RelativeLayout。此图像是健康计划卡的图像。我必须使用用户数据填充卡片,因此我必须在特定位置放置卡片(ImageView)上的一些TextView。

图像视图示例:

|------------------------------------------|
|                                          |
| Name:                                    |
|                                          |
| Card Number:                             |
|                                          |
|------------------------------------------|

将TextView放在图像上后的图像视图示例:

|------------------------------------------|
|                                          |
| Name: Josh Smith                         |
|                                          |
| Card Number: 010.251.526-50              |
|                                          |
|------------------------------------------|

该应用程序将用于不同的屏幕尺寸,因此我必须将这些TextView相对于ImageView定位。在该示例中,我必须将用户的名称写在图像顶部的近30%和图像的左角的20%,因为如果在更改智能手机的方向后调整图像的大小,则名称仍将正确定位。

非常感谢您的帮助,并对我的英语感到抱歉。

2 个答案:

答案 0 :(得分:2)

以下是名称的xml代码,如果top是30%而左边是20%

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backgroundImage"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.7"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.2" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.8" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="User Name" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

答案 1 :(得分:0)

使用背景文本通常不是一个好习惯,我建议避免这种情况有很多原因。你需要做的事情:

  • 在单独的文件夹中定义多个布局,例如layout-large,layout-small ....
  • 创建TextViews“name”和“number”并在Text XML属性中写入“Name:”和“Card number:”文本

然后您可以将变量附加到这些视图。例如:

 TextView txtName = (TextView)findViewById(R.id.name);
 TextView txtNumber = (TextView)findViewById(R.id.number);
 String Name = "John";
 String number = "12345";

txtName.setText(txtName.getText() + Name);
txtNumber.setText(txtNumber.getText() + number);