【发布时间】:2016-02-19 09:37:01
【问题描述】:
我正在开发一个即时聊天应用程序。我必须实现以下屏幕。
在屏幕截图中,您可以看到我们有一个圆形图像视图,其中显示了用户的个人资料图片。在个人资料图片上,我们有一个绿色的小圆圈,表示用户在线。我正在使用以下xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/padding10">
<FrameLayout
android:id="@+id/frame"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/margin10">
<com.almabay.almachat.circularImageView.CircularImageView
android:id="@+id/imgContact"
android:layout_width="80dp"
android:layout_height="80dp "
android:layout_gravity="bottom|left" />
<com.almabay.almachat.circularImageView.CircularImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="top|right"
android:background="#78dd63" />
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin10"
android:layout_toRightOf="@+id/frame"
android:orientation="vertical"
android:padding="@dimen/padding5">
<TextView
android:id="@+id/txtNam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Neeraj"
android:textSize="15sp" />
<TextView
android:id="@+id/stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin10"
android:text="Welocme to Almachat" />
</LinearLayout>
</RelativeLayout>
但我得到的屏幕如下所示:
在这里您可以看到绿色不是以圆形显示。它以方形显示。但是我在设计中设置了颜色,在实际实践中我想在用户将其设置为绿色时使用 java 代码在线。我已经采取了圆形图像视图。仍然背景颜色显示为正方形。请指导我如何解决此问题。
解决了问题:
创建一个名为 circle_green.xml 的可绘制对象
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="270"
android:endColor="@color/green_color"
android:startColor="@color/green_color" />
<stroke
android:width="2dp"
android:color="@color/while_color"></stroke>
</shape>
在图像视图的背景中使用它
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/padding10">
<com.almabay.almachat.circularImageView.CircularImageView
android:id="@+id/imgContact"
android:layout_width="80dp"
android:layout_height="80dp "
android:layout_alignParentLeft="true" />
<ImageView
android:id="@+id/online"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignRight="@+id/imgContact"
android:layout_alignTop="@+id/imgContact"
android:background="@drawable/circle_green" />
它现在对我有用。
【问题讨论】:
-
使用相对布局而不是 FrameLayout
-
@Madhur:你能详细解释一下吗?
-
建议你相同(可绘制文件作为背景)