简体   繁体   English

如何在android中放置几个​​文本视图和图像视图?

[英]How to put several text views over and image view in android?

I want to achieve following effect in my android app: 我想在我的Android应用程序中实现以下效果:

在此输入图像描述

As background I use a nine-patch png image. 作为背景我使用九补丁png图像。 I tried it like this with just one text view 我只用一个文本视图就这样试了

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/rowimage" />

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/myImageView"
    android:layout_alignTop="@+id/myImageView"
    android:layout_alignRight="@+id/myImageView"
    android:layout_alignBottom="@+id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />

But this is what I get as result 但这就是我得到的结果

在此输入图像描述

Any ideas whats wrong? 任何想法都错了吗? How can I achieve the effect from the first image? 如何从第一张图片中获得效果?

Edit: 编辑:

I updated my xml to relative layout, now I get following result 我将我的xml更新为相对布局,现在我得到了以下结果

在此输入图像描述

Why does the nine-patch image not scale with the text?? 为什么9补丁图像不能与文本一起缩放?

The first Solution try to add a RelativeLayout which is setted to fill_parent in width , and set your ninePatch image as a background for it , and then add your TextViews into it like this : 第一个解决方案尝试添加一个在宽度上设置为fill_parentRelativeLayout ,并将ninePatch图像设置为background ,然后将TextViews添加到其中,如下所示:

<RelativeLayout 
  android:id="@+id/layoutTextViews"  
  android:layout_width="fill_parent"
  android:layout_height = "wrap_content"
  android:background="@drawable/rowimage" 
>

<TextView
    android:id="@+id/txtView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />

<TextView
    android:id="@+id/txtView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Right"
    android:textColor="#000000" />

</RelativeLayout>

use this layout 使用这个布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <ImageView android:src="@drawable/icon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true"/>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true">
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Left Text" android:layout_weight="1.0" />
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Right Text" android:layout_weight="1.0" />
    </LinearLayout>
</RelativeLayout>

Answers have already been posted, but this is how I would do it: 答案已经发布,但我会这样做:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:padding="1dp"
    android:background="@drawable/tile">
    <TextView
        style="@style/textstyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello1"/>
    <View 
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <TextView
        style="@style/textstyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello2"/>
</LinearLayout>

The LinearLayout uses the ninepatch as a background so that it stretches with the content, The view in the middle stretches to create a gap between text1 and text2. LinearLayout使用ninepatch作为背景,以便它与内容一起伸展,中间的视图伸展以在text1和text2之间创建间隙。

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

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="horizontal" 
             android:layout_width="fill_parent"
             android:layout_height="50dp"
             android:background="@drawable/your background image">


        <LinearLayout 
            android:layout_height="fill_parent"
            android:layout_width="fill_parent" 
            android:layout_weight="1"
            android:gravity="center" 
            android:orientation="vertical">

            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textColor="@color/white"
                android:textSize="18sp" 
                android:textStyle="bold"
                android:singleLine="true" 
                android:text="Subject1"/>
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textColor="@color/white"
                android:textSize="18sp" 
                android:textStyle="bold"
                android:singleLine="true" 
                android:text="Subject2"/>
             <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textColor="@color/white"
                android:textSize="18sp" 
                android:textStyle="bold"
                android:singleLine="true" 
                android:text="Subject3"/>

        </LinearLayout>

    </LinearLayout>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

<ImageView android:src="@drawable/icon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true"/>
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Left Text" android:layout_weight="1.0" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Right Text" android:layout_weight="1.0" />

</Linear Layout >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM