티스토리 뷰
TextSwitcher 사용
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity">
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textSwitcher"
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private TextSwitcher textSwitcher;
private Button nextButton;
private int stringIndex = 0;
private String[] row = {"ONE", "TWO", "THREE", "FORE", "FIVE"};
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textSwitcher = findViewById(R.id.textSwitcher);
nextButton = findViewById(R.id.button);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (stringIndex == row.length - 1) {
stringIndex = 0;
textSwitcher.setText(row[stringIndex]);
} else {
textSwitcher.setText(row[++stringIndex]);
}
}
});
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
textView = new TextView(MainActivity.this);
textView.setTextColor(Color.BLACK);
textView.setTextSize(60);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
return textView;
}
});
textSwitcher.setText(row[stringIndex]);
}
}
'android' 카테고리의 다른 글
spannable string (0) | 2018.10.23 |
---|---|
Remove the ActionBar (0) | 2018.10.16 |
FilterRecyclerView (0) | 2018.10.05 |
BottomNavigationView (0) | 2018.10.04 |
Navigation - Part 2 (0) | 2018.09.28 |