我想在Android中创建一个功能齐全的视图,可以根据某些配置在特定的时间段内自行更新。
我从RelativeLayout继承了我的视图,我自动在布局中放置了TextView,然后通过发布处理程序,更新TextView边距以在RelativeLayout中移动文本。
自定义视图的代码是(我删除了不必要的行):
import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.util.AttributeSet
import android.widget.RelativeLayout
import android.widget.TextView
import blah.blah.test.R
class MovingText : RelativeLayout, Runnable {
private val tag = MarqueuWidget::class.java.simpleName
private lateinit var mTextField: TextView
private lateinit var refreshHandler: Handler
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
extractAttributes(attrs)
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int)
: super(context, attrs, defStyleAttr) {
init()
extractAttributes(attrs)
}
@SuppressLint("ResourceType")
private fun init() {
// init helper
mTextField = TextView(context)
mTextField.setSingleLine(false)
mTextField.setBackgroundColor(R.color.colorPrimary)
val lp = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
mTextField.id = 1
addView(mTextField, lp)
refreshHandler = Handler()
refreshHandler.postDelayed(this, 1000)
}
/**
* Runner for refresh handler
*/
override fun run() {
val params = mTextField.layoutParams as LayoutParams
params.leftMargin += 20
params.topMargin += 20
mTextField.layoutParams = params
refreshHandler.postDelayed(this, 1000)
}
@SuppressLint("Recycle")
private fun extractAttributes(attrs: AttributeSet) {
if (context == null) {
return
}
val a = context.obtainStyledAttributes(attrs,
R.styleable.blah_blah_widget_MovingText) ?: return
// some codes
repaint()
a.recycle()
}
private fun repaint() {
// sophisticate text view based on parameters
mTextField.text = "Test"
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
if (!changed) return
if (childCount != 1 || getChildAt(0) !is TextView) {
throw RuntimeException("TextChunk must not have any child element.")
}
if (getChildAt(0) !is TextView) {
throw RuntimeException("The child view of this chunk must be a TextView instance.")
}
super.onLayout(changed, l, t, r, b)
}
}
尽管相对布局参数(marginTop和marginLeft)随时间变化,但文本不会在视图内移动。有什么问题?
此代码对我有用:
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.RelativeLayout
import android.widget.TextView
import butterknife.ButterKnife
class TestActivity : AppCompatActivity(), Runnable {
override fun run() {
handler.postDelayed(this, 100)
var txt = findViewById<TextView>(R.id.textView1)
val params = txt.layoutParams as RelativeLayout.LayoutParams
params.leftMargin += 20
params.topMargin += 20
txt.layoutParams = params
}
lateinit var handler: Handler
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
ButterKnife.bind(this)
handler = Handler()
handler.postDelayed(this, 1000)
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="800px"
android:layout_height="wrap_content"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"
android:layout_marginStart="-20dp"
android:layout_marginTop="-10dp"
android:ellipsize="none"
android:singleLine="true"
android:text="daskdjasgdasgdhjasd hasfdhfasghdfash gjdfashjfdhas" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)