主布局文件:
<?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"
android:background="@color/colorGray"
android:orientation="vertical"
>
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
android:onClick="next"
/>
</LinearLayout>
主程序文件的内容如下:
package com.toby.personal.testlistview;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class MainActivity extends AppCompatActivity {
final private static String TAG = "Toby_Test";
private TextSwitcher textSwitcher = null;
final String[] strs = new String[] {
"C++ 多态性 运算符重载",
"C++ 多态性 虚函数、抽象类(一)",
"C++ 多态性 虚函数、抽象类(二)",
"C++ 作用域分辨符"
};
private int times = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView tv = new TextView(MainActivity.this);
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
tv.setTextColor(Color.MAGENTA);
return tv;
}
});
next(null);
}
public void next(View view) {
textSwitcher.setText(strs[times++ % strs.length]);
}
}
本示例的实际运行效果,各位可以自行运行测试.
参考文献:《疯狂Android讲义(第2版)》
网友评论