我尝试动态创建文本视图,我实现了类似的功能。有人可以帮助我实现类似于上面的图像的样式?
以下是我用来实现上述风格的代码块。
chat_bubble.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#D8000000" />
<corners android:bottomLeftRadius="1dp" android:radius="4dp" />
</shape>
Activity.kt
fun createTextView(activity: PlacesActivity){
var textView = TextView(this)
textView.setText("Text ABCDD")
textView.textSize = 24F
textView.alpha = 0F
textView.setTextColor(Color.MAGENTA)
textView.setBackgroundResource(R.drawable.chat_bubble)
textView.setPadding(10,10,10,10)
var params =
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(50,50,50,50)
textView.setLayoutParams(params)
linearLayout.addView(textView, params);
fadeMarker(textView)
}
提前致谢。
答案 0 :(得分:1)
为聊天气泡背景创建一个形状:
shape_pill_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="40dp"
android:bottomRightRadius="40dp"
android:radius="40dp"
android:topLeftRadius="40dp"
android:topRightRadius="40dp" />
<solid android:color="#555555" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size android:height="40dp" />
</shape>
创建你的箭头drawable:
shape_arrow_down.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="230%"
android:pivotY="30%"
android:toDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#555555" />
</shape>
</rotate>
</item>
</layer-list>
找到最能代表你的“黄金”箭头的资产,我拿了内置的android studio vector资产,这将被称为ic_arrow_forward_wattle_24dp.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayoutTextBubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/shape_pill_background"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textViewInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#919191"
android:padding="4dp"
android:text="This is a text message"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageViewArrow"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageViewArrow"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="@+id/textViewInput"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textViewInput"
app:layout_constraintTop_toTopOf="@+id/textViewInput"
app:srcCompat="@drawable/ic_arrow_forward_wattle_24dp" />
</android.support.constraint.ConstraintLayout>
<View
android:id="@+id/viewDownArrow"
android:layout_width="22dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="@drawable/shape_arrow_down"
app:layout_constraintEnd_toEndOf="@+id/constraintLayoutTextBubble"
app:layout_constraintStart_toStartOf="@+id/constraintLayoutTextBubble"
app:layout_constraintTop_toBottomOf="@+id/constraintLayoutTextBubble" />
</android.support.constraint.ConstraintLayout>
结果如下:
然而,这有一些问题。当你有一个相当大的消息时,它会将箭头ImageView推出泡泡,因为wrap_content在链和约束布局中的工作方式。我建议你尝试给你的消息TextView一个固定的宽度或设置它的宽度以匹配约束。这将为您的文本提供没有文本的灰色背景。玩弄它,看看什么最适合你。