在 TextView 的背景上获取文本

get Text on TextView's background

谁能告诉我如何在 TextView 的文本上获取背景图片 像这样

我可以通过 TextView 获取背景图片,但图片在 textView 下方显示如下

谁能帮帮我

提前致谢

使用 Frame LayoutTextView 上方放置一个 ImageView。 希望这就是您所要求的。

或者像这样使用相对布局

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"/>
   <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"/>

  </RelativeLayout>

并将图像设置为此图像视图将显示在文本视图上方。

试试这个代码: 将您的背景设置为 RelativeLayout.

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

你可以像这样设置文本视图的背景:

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ur_background_image"
    android:padding="10dp"
    android:text="30%" />

请试试这个希望对你有帮助

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

     <ImageView
         android:id="@+id/imageView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:src="@drawable/abc_ab_solid_light_holo" />

     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_centerVertical="true"
         android:text="TextView" />

    </RelativeLayout>
    
</LinearLayout>

1) 我不知道你打算做什么,但这看起来像 ProgressBar

2) 但如果你想坚持使用这种配置,请使用这种布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:paddingTop="16dp"
                android:paddingBottom="16dp"
                tools:context="com.example.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignTop="@+id/textViewPercentage"
        android:layout_alignLeft="@+id/textViewPercentage"
        android:layout_alignRight="@+id/textViewPercentage"
        android:layout_alignBottom="@+id/textViewPercentage"
        android:weightSum="10"
        >
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:id="@+id/imageViewProgress"
            android:background="#0F0"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="7"
            android:id="@+id/imageViewTotal"
            android:background="#000"/>
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="30%"
        android:ems="10"
        android:gravity="center"
        android:textColor="#FFF"
        android:id="@+id/textViewPercentage"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>

在您的 Java 代码中,使用它来更改文本:

((TextView) findViewById(R.id.textViewPercentage)).setText("10%");

使用它来改变栏内的 "percentage" 颜色:

int weight = 5; // put a number between 0 and 10 here

LinearLayout.LayoutParams myLayoutParamsProgress = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
myLayoutParamsProgress.weight = weight;
LinearLayout.LayoutParams myLayoutParamsTotal = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
myLayoutParamsTotal.weight = 10 - weight;

findViewById(R.id.imageViewProgress).setLayoutParams(myLayoutParamsProgress);
findViewById(R.id.imageViewTotal).setLayoutParams(myLayoutParamsTotal);