java - How to Overlay text over image at left top corner
问题描述
I want to have textview over image at top left corner of the Imageview similar to this. how can I do this.
I have this code but It doesn't do what i want.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<ImageView
android:id="@+id/myImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/myImageSouce"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_medium"
android:layout_gravity="center"
tools:text="Test"/>
</FrameLayout>
Thanks for help in advance.
解决方案
Try using a ConstraintLayout
. Here's a simple example. For layering, views are layered in the order listed in the xml so if you want the TextView
in front put it last in the xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:srcCompat="@drawable/ic_home_black_24dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Words"
android:padding="20dp"
android:background="#00ff00"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
推荐阅读
- talend - 哪个用户使用 Windows 身份验证在 Windows 任务计划程序中连接 MSSQL
- angular - 如何在Angular中基于Request Uri在自定义拦截器和Msal拦截器之间使用条件拦截器
- python - theano:抱歉,未实现:未编译 64 位模式
- visual-studio - 以编程方式创建解决方案
- scrapy - 抓取 5264 页(0 页/分钟),抓取 6302 条(0 条/分钟)
- pyomo - GAMS 转换的非线性目标函数看起来与定义的目标不同
- hover - plotly Scatter customdata oare only NaN
- c++ - 如何找到内置问题匹配器的定义?
- python - 从 xarray 获取 Python 中特定位置的土壤水分数据
- geospatial - 如何从空间特征中检索 osmdata 的 bbox?