我一直试图在我的应用程序中为单词 HELLO 提供选取框效果,但除非文本长度超过屏幕大小,否则 android 不允许这样做。有解决方案吗?
PS:看起来很简单,我还没有找到任何解决方案。
我一直试图在我的应用程序中为单词 HELLO 提供选取框效果,但除非文本长度超过屏幕大小,否则 android 不允许这样做。有解决方案吗?
PS:看起来很简单,我还没有找到任何解决方案。
我使用了我在早期 Android 时代开发的简单的轻量级自动收报机动画。
下面是完整的代码。
希望这可以帮助。
适用于 Android 的所有 API 级别
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setticker("Hello", this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void setticker(String text, Context contx) {
if (text != "") {
LinearLayout parent_layout = (LinearLayout) ((Activity) contx)
.findViewById(R.id.ticker_area);
TextView view = new TextView(contx);
view.setText(text);
view.setTextColor(Color.BLACK);
view.setTextSize(25.0F);
Context context = view.getContext(); // gets the context of the view
// measures the unconstrained size of the view
// before it is drawn in the layout
view.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
// takes the unconstrained width of the view
float width = view.getMeasuredWidth();
float height = view.getMeasuredHeight();
// gets the screen width
float screenWidth = ((Activity) context).getWindowManager()
.getDefaultDisplay().getWidth();
view.setLayoutParams(new LinearLayout.LayoutParams((int) width,
(int) height, 1f));
System.out.println("width and screenwidth are" + width + "/"
+ screenWidth + "///" + view.getMeasuredWidth());
// performs the calculation
float toXDelta = width - (screenWidth - 0);
// sets toXDelta to -300 if the text width is smaller that the
// screen size
if (toXDelta < 0) {
toXDelta = 0 - screenWidth;// -300;
} else {
toXDelta = 0 - screenWidth - toXDelta;// -300 - toXDelta;
}
// Animation parameters
Animation mAnimation = new TranslateAnimation(screenWidth,
toXDelta, 0, 0);
mAnimation.setDuration(15000);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
view.setAnimation(mAnimation);
parent_layout.addView(view);
}
}
}
在activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/ticker_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#9CB1DD"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
有一个简单的解决方案
您必须像下面的代码一样创建WebView
并显示选取框。
webView = (WebView)findViewById(R.id.web);
String summary = "<html><FONT color='#fdb728' FACE='courier'><marquee behavior='scroll' direction='left' scrollamount=10>"
+ "Hello Droid" + "</marquee></FONT></html>";
webView.loadData(summary, "text/html", "utf-8");
尝试使用 Git Hub 中的Tickerview。
这是您问题的完整代码:
Main_Activity.java 代码:'
WebView webView;
webView = (WebView)findViewById(R.id.web);
String summary = "<html><FONT color='#fdb728' FACE='courier'><marquee behavior='scroll' direction='left' scrollamount=10>"
+ "Hello Droid" + "</marquee></FONT></html>";
webView.loadData(summary, "text/html", "utf-8"); // Set focus to the textview'
main_activity.xml 代码:
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/web"
></WebView>
为了在文本视图上应用 tickr/marquee 动画(即使是短文本长度),它会创建一个从极右 (+1f) 到极左 (-1f) 的平移动画,然后重新启动动画。
public void setTickerAnimation(View view) {
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, +1f,
Animation.RELATIVE_TO_SELF, -1f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.RESTART);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(4000);
view.startAnimation(animation);
}
我发现的捷径是调用这个方法并传递TextView
给它。
fun addMarquee(textView: TextView) {
textView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val pixels = textView.measuredWidth - 1
val params = textView.layoutParams
params.width = pixels
textView.layoutParams = params
textView.isSelected = true
textView.ellipsize = TextUtils.TruncateAt.MARQUEE
textView.isSingleLine = true
textView.marqueeRepeatLimit = -1
textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
注意:此方法仅在 textView 宽度为wrap_content
.