跑马灯显示
android:ellipsize="marquee" android:singleLine="true"
paomad.setSelected(true);
使用RelativeLayout可以使用图标点击旋转,展开textview或收缩textview
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="15dp"
android:layout_marginRight="6dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/onete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="@string/wgms"
android:textColor="@color/numtext"
android:textSize="15dp" />
<TextView
android:id="@+id/wgmiaostext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:ellipsize="end"
android:lineSpacingExtra="4dp"
android:lines="2"
android:text="申报过期申报报过期申报过期申过期申报过期申报过期申报报报报报过期申报过期"
android:textColor="@color/hometextc"
android:textSize="15dp" />
<ImageView
android:id="@+id/more_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:src="@mipmap/xj_03"/>
</RelativeLayout>
Java代码如下
private boolean ifupdown=true;
@OnClick({R.id.backligxwbdata,R.id.more_image})
public void onClick(View view) {
switch (view.getId()) {
case R.id.backligxwbdata:
onBackPressed();
break;
case R.id.more_image:
if (ifupdown){
Animation anim =new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setFillAfter(true); // 设置保持动画最后的状态
anim.setDuration(500); // 设置动画时间
anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
more_image.startAnimation(anim);//图标旋转向上或向下
ifupdown = !ifupdown;
double d = (double) weiguidesc.length() / 18;//文本长度除以每行字符长度
int okcprogress = (int) (Math.floor(d))+1;//除数取整,也就是行数
Log.i("lgq","lllll===-"+weiguidesc.length()+"......."+okcprogress);
wgmiaostext.setLines(okcprogress);//展开全部
}else {
Animation anim =new RotateAnimation(180f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setFillAfter(true); // 设置保持动画最后的状态
anim.setDuration(500); // 设置动画时间
anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
more_image.startAnimation(anim);
wgmiaostext.setLines(2);//收缩为原始两行 ifupdown = !ifupdown;
}
break;
}
}
实现展开收起方法2:
(1)资源
<color name="color_8290AF">#8290AF</color> <color name="color_232323">#232323</color>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ExpandTextView">
<attr name="showLines" format="integer"/>
</declare-styleable>
</resources>
layout_expand_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/contentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/color_232323"
android:textSize="14sp"
android:text=""
/>
<TextView
android:id="@+id/textState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/color_8290AF"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text=""
/>
</LinearLayout>
(2)自定义textview
public class ExpandTextView extends LinearLayout {
public static final int DEFAULT_MAX_LINES = 3;//最大的行数
private TextView contentText;
private TextView textState;
private int showLines;
private ExpandStatusListener expandStatusListener;
private boolean isExpand;
public ExpandTextView(Context context) {
super(context);
initView();
}
public ExpandTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
initView();
}
public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs);
initView();
}
private void initView() {
setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(getContext()).inflate(R.layout.layout_expand_text, this);
contentText = (TextView) findViewById(R.id.contentText);
contentText.setLineSpacing(8,1);//行间距
contentText.setLetterSpacing(0.5f); //字间距
if(showLines > 0){
contentText.setMaxLines(showLines);
}
textState = (TextView) findViewById(R.id.textState);
textState.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String textStr = textState.getText().toString().trim();
if("全文".equals(textStr)){
contentText.setMaxLines(Integer.MAX_VALUE);
textState.setText("收起");
setExpand(true);
}else{
contentText.setMaxLines(showLines);
textState.setText("全文");
setExpand(false);
}
//通知外部状态已变更
if(expandStatusListener != null){
expandStatusListener.statusChange(isExpand());
}
}
});
}
private void initAttrs(AttributeSet attrs) {
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ExpandTextView, 0, 0);
try {
showLines = typedArray.getInt(R.styleable.ExpandTextView_showLines, DEFAULT_MAX_LINES);
}finally {
typedArray.recycle();
}
}
public void setText(final CharSequence content){
contentText.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
// 避免重复监听
contentText.getViewTreeObserver().removeOnPreDrawListener(this);
int linCount = contentText.getLineCount();
if(linCount > showLines){
if(isExpand){
contentText.setMaxLines(Integer.MAX_VALUE);
textState.setText("收起");
}else{
contentText.setMaxLines(showLines);
textState.setText("全文");
}
textState.setVisibility(View.VISIBLE);
}else{
textState.setVisibility(View.GONE);
}
return true;
}
});
contentText.setText(content);
}
public void setExpand(boolean isExpand){
this.isExpand = isExpand;
}
public boolean isExpand(){
return this.isExpand;
}
public void setExpandStatusListener(ExpandStatusListener listener){
this.expandStatusListener = listener;
}
public static interface ExpandStatusListener{
void statusChange(boolean isExpand);
}
}
调用:
<com.example.my35.ExpandTextView
android:id="@+id/expand_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ellipsize="end"
android:lineSpacingExtra="3dp"
android:maxLines="5"
android:textSize="16sp"
app:layout_constraintRight_toRightOf="parent"
/>
ExpandTextView textView1 =findViewById(R.id.expand_textView);
textView1.setText("分为非我仿佛威风威风为任务范围范围分为非危房危房危房威锋网分为非仍无法" +
"为非危房危房危房威锋网分为非仍无法危房违法未范围范围分为非危房危房任务分为分为分为我分为非我仿佛威风威" +
"风为任务范围范围分为非危房危房危房威锋网分为非仍无法危房违法未范围范围分为非危房危房任务分为分为分为我");
单行省略
android:singleLine="true"
android:ellipsize="end"
多行省略
android:maxLines="3"
android:ellipsize="end"
行距
android:lineSpacingExtra="6dp"
contentText.setLineSpacing(8,1);//行间距 contentText.setLetterSpacing(0.5f); //字间距

本文介绍如何在Android中实现TextView的跑马灯效果、点击展开/收起文本及图标旋转动画,并提供了两种实现方法的详细代码示例。
3478





