【问题标题】:How to place rounded textview with image near to transparent image android?`如何将带有图像的圆形文本视图放置在透明图像android附近?`
【发布时间】:2015-01-05 08:08:48
【问题描述】:
如何将带有两个文本视图的圆形图像放置在靠近透明图像的下方?我用 textview 创建了透明图像,但我不知道如何用两个 textview 放置圆形图像,即日期和日期。
如何达到预期的输出?
预期输出是:
这是我的代码。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAA" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlay_margin"
android:background="#FFF" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/salogo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#7000"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingTop="15dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Marathon for organ donation"
android:textColor="#CCC"
android:textSize="12sp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
但我得到的输出是:
【问题讨论】:
标签:
android
android-layout
android-imageview
android-relativelayout
【解决方案1】:
在可绘制文件夹中创建circular_bg.xml
并写..
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:endColor="#3399FF"
android:startColor="#3399FF" />
<padding
android:bottom="1.5dp"
android:left="1.5dp"
android:right="1.5dp"
android:top="1.5dp" />
</shape>
现在在你的 xml 中
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="2dp" >
<TextView
android:id="@+id/text"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="31 sun"
android:layout_centerInParent="true"
android:background="@drawable/circular_bg"
android:gravity="center"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
它会解决你的问题。
【解决方案2】:
你也可以用你当前的布局来实现几乎这样的效果......这是你相对布局的一个补充,一个圆形的 textView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlay_margin"
android:background="#FFF" >
<ImageView
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:src="@drawable/salogo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#7000"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingTop="15dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Marathon for organ donation"
android:textColor="#CCC"
android:textSize="12sp"/>
</LinearLayout>
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:background="@drawable/circle" />
</RelativeLayout>
对于圆形,通过在您的可绘制文件夹中创建一个名为 circle 的 xml 文件来添加以下代码。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<corners android:radius="10dp" />
<!-- this is for the outline you can remove this stroke or change the color by replacing #FFFFFF with your color -->
<stroke android:width="1dp"
android:color="#FFFFFF" />
</shape>