日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android浮动文本,android 添加浮动标签在textView最尾端,自动换行

發布時間:2025/5/22 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android浮动文本,android 添加浮动标签在textView最尾端,自动换行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.文章背景

最近在做一個需求,效果圖如下:先上張圖

image

就是動態根據textView文本,追隨一個標簽在 最后面~

其實代碼也很簡單,就是動態計算textView文本的寬度 和 標簽的寬度,

如何兩個之和 大于父控件的寬度,這時候件需要把標簽的位置改變,

這邊我們可以使用自定義ViewGroup,然后通過layout(l,t,r,b) or 是通過topMargin 來控制~

下面我們來看看整體代碼:

### MainActivity::

class FollowActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_follow)

text1.text = "您已經成功達到活動入倉數量,11-11-11"

text2.text = "您已經成功達到活動入倉數量,11-11-11出售制定活動"

text3.text = "您已經成功達到活動入倉數量,11-11-11出售制定活動商品可分享,11出售制定活動商品可分享"

}

}

### xml

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"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"

android:orientation="vertical"

tools:context=".aes.FollowActivity">

android:id="@+id/text1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="200dp" />

android:id="@+id/text2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="100dp" />

android:id="@+id/text3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="100dp" />

下面看看最關鍵的類,我們自己定義的ViewGroup

### FollowView.java

class FollowView @JvmOverloads constructor(

context: Context,

attrs: AttributeSet? = null,

defStyleAttr: Int = 0

) : FrameLayout(context, attrs, defStyleAttr) {

companion object {

const val TAG = "FollowView"

}

private var IActivityRuleClick: (() -> Unit)? = null

init {

LayoutInflater.from(context).inflate(R.layout.layout_follow_view, this, true)

activityRules.setOnClickListener {

Log.e(TAG, "onclick rules")

IActivityRuleClick?.invoke()

}

}

var text: String?

get() = activityTitle.text.toString()

set(value) {

if (activityTitle.text != value) {

activityTitle.text = value

}

}

private fun getMaxTextHeight(layout: Layout, maxLine: Int): Int {

val fm = activityTitle.paint.fontMetrics

val lineBottom = if (maxLine > 1) {

layout.getLineBottom(maxLine - 2) + (fm.bottom - fm.top).toInt()

} else {

layout.getLineBottom(maxLine - 1)

}

return lineBottom + activityTitle.paddingTop + activityTitle.paddingBottom

}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec)

val parentWidth = MeasureSpec.getSize(widthMeasureSpec);

val titleLayout = activityTitle.layout

val rulesLayout = activityRules.layout

if (titleLayout == null || rulesLayout == null) {

return

}

val lineCount = titleLayout.lineCount

val titleLayoutWidth = titleLayout.getLineWidth(0)

val rulesLayoutWidth = rulesLayout.getLineWidth(0)

if ((titleLayoutWidth + rulesLayoutWidth) >= parentWidth) {

Log.e(TAG, "textView Width > screen Width")

val contentWidthSpec = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY)

val contentHeightSpec = MeasureSpec.makeMeasureSpec(

getMaxTextHeight(

activityTitle.layout,

if (lineCount == 1) lineCount + 1 else lineCount

),

MeasureSpec.EXACTLY

)

activityTitle.measure(contentWidthSpec, contentHeightSpec)

val parentHeight = activityTitle.measuredHeight + paddingTop + paddingBottom

Log.e(

TAG,

"textWidth = " + activityTitle.measuredWidth + " textHeight = " + activityTitle.measuredHeight

)

/**

* action(1)

* 使用topMargin的方式來控制,追加的標簽換行

*/

/*

val layoutParams = activityRules.layoutParams as LayoutParams

layoutParams.topMargin = parentHeight - activityRules.measuredHeight

activityRules.layoutParams = layoutParams

*/

setMeasuredDimension(parentWidth, parentHeight)

}

}

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {

super.onLayout(changed, l, t, r, b)

val left = activityTitle.measuredWidth - activityRules.measuredWidth

val top = activityTitle.measuredHeight - activityRules.measuredHeight

val right = activityTitle.measuredWidth

val bottom = activityTitle.measuredHeight

/**

* action(2)

* 使用layout的方式來控制,追加的標簽換行

*/

activityRules.layout(left, top, right, bottom)

Log.e(TAG, "l = $l,t = $t, r = $r, b = $b")

}

override fun generateDefaultLayoutParams(): LayoutParams {

return super.generateDefaultLayoutParams()

}

override fun generateLayoutParams(lp: ViewGroup.LayoutParams?): ViewGroup.LayoutParams {

return super.generateLayoutParams(lp)

}

override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {

return super.generateLayoutParams(attrs)

}

### 我們這邊使用了一個布局,(layout_follow_view)布局當中就是TextView的文本,和 標簽view...方便用戶改成圖片或者其他的標簽

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/activityTitle"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:lineHeight="20dp"

android:textColor="#535365"

android:textSize="12dp"

tools:text="您已經成功達到活動入倉數量,11-11-11出售制定活動商品可分享" />

android:id="@+id/activityRules"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="right"

android:text="活動規則 > "

android:textColor="#FFAAAABB"

android:textSize="12dp"

android:textStyle="bold" />

整體就說完了,這種場景在做需求中還是很多場景的,所以這邊就記錄下來,分享給更多人!同時自己也對ViewGroup的 計算寬高,和重新定位某個子View的位置 復習了下!!

總結

以上是生活随笔為你收集整理的android浮动文本,android 添加浮动标签在textView最尾端,自动换行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。