下面列出了android.widget.TextView#setTextAlignment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Factory method to initialize each text view and add it to the layout
* @param view text view to be initialized
* @param layout layout to which the view is to be added
* @param columnId column of the layout to which the view is to be added
*/
private void initializeTextView(
TextView view,
GridLayout layout,
int columnId){
layout.addView(view);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = GridLayout.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
view.setTextSize(11);
view.setWidth(TEXT_FIELD_WIDTH[columnId]);
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(columnId);
param.rowSpec = GridLayout.spec(rowId);
view.setLayoutParams (param);
}
private void initializeTextView(
TextView view,
GridLayout layout,
int columnId){
layout.addView(view);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = GridLayout.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
view.setTextSize(11);
view.setWidth(TEXT_FIELD_WIDTH[columnId]);
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(columnId);
param.rowSpec = GridLayout.spec(rowId);
view.setLayoutParams (param);
}
private View getSingleTab(int position) {
LinearLayout view = new LinearLayout(getContext());
view.setClickable(true);
view.setGravity(Gravity.CENTER);
view.setBackgroundResource(CLICK_BG_RES);
view.setOrientation(LinearLayout.VERTICAL);
//图标
ImageView iconView = new ImageView(getContext());
//三个参数的构造可以设置权重
iconView.setLayoutParams(new LayoutParams(SIZE_ICON, SIZE_ICON));
iconView.setImageResource(iconsUnselect[position]);
iconView.setColorFilter(COLOR_UNSELECT);
//标题
TextView textView = new TextView(getContext());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
textView.setTextAlignment(TEXT_ALIGNMENT_CENTER);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(COLOR_UNSELECT);
textView.setText(tabNames[position]);
view.addView(iconView);
view.addView(textView);
// 返回布局视图
return view;
}
/**
* 设置TextView(@+id/snackbar_text)中文字的对齐方式 居中
*
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public SnackbarUtils messageCenter() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (getSnackbar() != null) {
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
//View.setTextAlignment需要SDK>=17
message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
message.setGravity(Gravity.CENTER);
}
}
return this;
}
/**
* 设置TextView(@+id/snackbar_text)中文字的对齐方式 居右
*
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public SnackbarUtils messageRight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (getSnackbar() != null) {
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
//View.setTextAlignment需要SDK>=17
message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
message.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
}
}
return this;
}
/**
* 设置TextView(@+id/snackbar_text)中文字的对齐方式 居中
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public SnackbarUtils messageCenter(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
if(getSnackbar()!=null){
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
//View.setTextAlignment需要SDK>=17
message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
message.setGravity(Gravity.CENTER);
}
}
return this;
}
/**
* 设置TextView(@+id/snackbar_text)中文字的对齐方式 居右
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public SnackbarUtils messageRight(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
if(getSnackbar()!=null){
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
//View.setTextAlignment需要SDK>=17
message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
message.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
}
}
return this;
}
/**
* @param activity Activity
* @param options Option[]
* @param owner Object
* @return TableRow
*/
public static TableRow createTable(Activity activity, Option[] options, Object owner)
{
TableRow tableRow = new TableRow(activity);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
//
LinearLayout linearLayout = new LinearLayout(activity);
linearLayout.setOrientation(LinearLayout.VERTICAL);
if (owner != null && (owner instanceof Transformer || owner instanceof Consumer))
{
//add divider
linearLayout.addView(Util.addDivider(activity));
}
TextView textViewName = new TextView(activity);
textViewName.setText(R.string.str_options);
textViewName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
textViewName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
linearLayout.addView(textViewName);
//
LinearLayout linearLayoutOptions = new LinearLayout(activity);
linearLayoutOptions.setBackgroundColor(activity.getResources().getColor(R.color.colorListBorder));
linearLayoutOptions.setOrientation(LinearLayout.VERTICAL);
//options
for (int i = 0; i < options.length; i++)
{
if (options[i].isAssignableByString())
{
linearLayoutOptions.addView(addOption(activity, options[i], owner));
}
}
linearLayout.addView(linearLayoutOptions);
tableRow.addView(linearLayout);
return tableRow;
}
/**
* 设置TextView(@+id/snackbar_text)中文字的对齐方式 居中
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public SnackbarUtils messageCenter(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
if(getSnackbar()!=null){
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
//View.setTextAlignment需要SDK>=17
message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
message.setGravity(Gravity.CENTER);
}
}
return this;
}
/**
* 设置TextView(@+id/snackbar_text)中文字的对齐方式 居右
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public SnackbarUtils messageRight(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
if(getSnackbar()!=null){
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
//View.setTextAlignment需要SDK>=17
message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
message.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
}
}
return this;
}
private static void setTextAlignment(android.support.design.widget.Snackbar snackbar) {
TextView textView = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
textView.setLayoutParams(params);
switch (textAlign) {
case CENTER:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
else
textView.setGravity(Gravity.CENTER_HORIZONTAL);
break;
case RIGHT:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
else
textView.setGravity(Gravity.RIGHT);
break;
case LEFT:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
else
textView.setGravity(Gravity.LEFT);
break;
default:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
else
textView.setGravity(Gravity.LEFT);
break;
}
}
/**
* 设置TextView(@+id/snackbar_text)中文字的对齐方式 居中
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public SnackbarUtils messageCenter(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
if(getSnackbar()!=null){
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
//View.setTextAlignment需要SDK>=17
message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
message.setGravity(Gravity.CENTER);
}
}
return this;
}
/**
* 设置TextView(@+id/snackbar_text)中文字的对齐方式 居右
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public SnackbarUtils messageRight(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
if(getSnackbar()!=null){
TextView message = (TextView) getSnackbar().getView().findViewById(R.id.snackbar_text);
//View.setTextAlignment需要SDK>=17
message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
message.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
}
}
return this;
}
private static void setPadding(Context context, TextView view) {
int padding = ControlHelper.dpToPx(2, context.getResources());
view.setPadding(padding, padding, padding, padding);
view.setGravity(Gravity.CENTER_VERTICAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
}
private void transform(Window window) {
try {
View sysContent = window.findViewById(Window.ID_ANDROID_CONTENT);
GradientDrawable backgroundDrawable = new GradientDrawable();
backgroundDrawable.setCornerRadius(ViewKnife.dip2px(10));
backgroundDrawable.setColor(Color.WHITE);
ViewCompat.setBackground(sysContent, backgroundDrawable);
DialogTitle title = window.findViewById(androidx.appcompat.R.id.alertTitle);
TextView message = window.findViewById(android.R.id.message);
Button button1 = window.findViewById(android.R.id.button1);
Button button2 = window.findViewById(android.R.id.button2);
Button button3 = window.findViewById(android.R.id.button3);
LinearLayout buttonParent = (LinearLayout) button1.getParent();
buttonParent.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
GradientDrawable verticalDrawable = new GradientDrawable();
verticalDrawable.setColor(0xffE5E5E5);
verticalDrawable.setSize(ViewKnife.dip2px(.5f), 0);
buttonParent.setDividerDrawable(verticalDrawable);
buttonParent.setPadding(0, 0, 0, 0);
GradientDrawable innerDrawable = new GradientDrawable();
innerDrawable.setStroke(ViewKnife.dip2px(.5f), 0xffE5E5E5);
InsetDrawable insetDrawable = new InsetDrawable(innerDrawable,
ViewKnife.dip2px(-1), 0, ViewKnife.dip2px(-1), ViewKnife.dip2px(-1));
ViewCompat.setBackground(buttonParent, insetDrawable);
window.findViewById(androidx.appcompat.R.id.spacer).setVisibility(View.GONE);
View textSpacerNoButtons = window.findViewById(androidx.appcompat.R.id.textSpacerNoButtons);
if (textSpacerNoButtons != null) {
textSpacerNoButtons.setVisibility(View.VISIBLE);
}
button1.setTextColor(0xff5B6B91);
button2.setTextColor(0xff353535);
button3.setTextColor(0xff353535);
button1.setPaintFlags(Paint.FAKE_BOLD_TEXT_FLAG);
button2.setPaintFlags(Paint.FAKE_BOLD_TEXT_FLAG);
button3.setPaintFlags(Paint.FAKE_BOLD_TEXT_FLAG);
((LinearLayout.LayoutParams) button3.getLayoutParams()).weight = 1;
((LinearLayout.LayoutParams) button2.getLayoutParams()).weight = 1;
((LinearLayout.LayoutParams) button1.getLayoutParams()).weight = 1;
if (message != null) {
message.setTextColor(0xff202020);
if (getArguments().getBoolean(ATTR7, false)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
message.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
message.setGravity(Gravity.CENTER_HORIZONTAL);
}
}
}
title.setTextColor(0xff353535);
title.setPaintFlags(Paint.FAKE_BOLD_TEXT_FLAG);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
title.setGravity(Gravity.CENTER_HORIZONTAL);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
@Override
protected void onCreate(Bundle saved)
{
super.onCreate(saved);
mHandler = new Handler();
game = loadFile();
actionInProgress = false;
setContentView(R.layout.activity_game);
RelativeLayout content = findViewById((R.id.content_layout));
if (saved == null) {
if ((game==null || getIntent().getExtras()!=null)) {
Bundle extras = getIntent().getExtras();
GameType gameType = GameType.valueOf(extras.getString("gameType", GameType.Bot.name()));
game = new CheckersGame(gameType,extras.getInt("level"));
}
}
maxDepth=game.searchDepth;
// else game = saved.getParcelable("gameController");
// generate new layout for the board
checkersView = new CheckersLayout(game, this);
checkersView.refresh();
// layouts which contain all items displayed ingame
LinearLayout mainContentLayout = findViewById(R.id.main_content);
LinearLayout sideContentLayout = findViewById(R.id.side_content);
mainContentLayout.addView(checkersView);
// text which displays whose turn it is
currentPlayerText = new TextView(this);
currentPlayerText.setTextSize(24);
currentPlayerText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
sideContentLayout.addView(currentPlayerText);
// layouts for captured pieces
capturedBlackPiecesUI = new LinearLayout(this);
capturedBlackPiecesUI.setOrientation(LinearLayout.HORIZONTAL);
capturedWhitePiecesUI = new LinearLayout(this);
capturedWhitePiecesUI.setOrientation(LinearLayout.HORIZONTAL);
sideContentLayout.addView(capturedBlackPiecesUI);
sideContentLayout.addView(capturedWhitePiecesUI);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(preferencesChangeListener);
}
/**
* @param activity Activity
* @param mainObject Object
* @param dividerTop boolean
* @return TableRow
*/
public static TableRow createStreamTable(Activity activity, final Object mainObject, boolean dividerTop, int heading)
{
TableRow tableRow = new TableRow(activity);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout linearLayout = new LinearLayout(activity);
linearLayout.setOrientation(LinearLayout.VERTICAL);
if (dividerTop)
{
//add divider
linearLayout.addView(Util.addDivider(activity));
}
TextView textViewName = new TextView(activity);
textViewName.setText(heading);
textViewName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
textViewName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
linearLayout.addView(textViewName);
//get possible providers
final Object[] objects = PipelineBuilder.getInstance().getPossibleStreamConnections(mainObject);
//
if (objects.length > 0) {
for (int i = 0; i < objects.length; i++) {
CheckBox checkBox = new CheckBox(activity);
checkBox.setText(objects[i].getClass().getSimpleName());
checkBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
Object[] providers = PipelineBuilder.getInstance().getStreamConnections(mainObject);
if (providers != null) {
for (Object provider : providers) {
if (objects[i].equals(provider)) {
checkBox.setChecked(true);
break;
}
}
}
final int count = i;
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
final Object o = objects[count];
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
PipelineBuilder.getInstance().addStreamConnection(mainObject, (Provider) o);
} else {
PipelineBuilder.getInstance().removeStreamConnection(mainObject, (Provider) o);
}
}
});
linearLayout.addView(checkBox);
}
} else
{
return null;
}
tableRow.addView(linearLayout);
return tableRow;
}
/**
* @param activity Activity
* @param mainObject Object
* @param dividerTop boolean
* @return TableRow
*/
public static TableRow createEventTable(Activity activity, final Object mainObject, boolean dividerTop)
{
TableRow tableRow = new TableRow(activity);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout linearLayout = new LinearLayout(activity);
linearLayout.setOrientation(LinearLayout.VERTICAL);
if (dividerTop)
{
//add divider
linearLayout.addView(Util.addDivider(activity));
}
TextView textViewName = new TextView(activity);
textViewName.setText(R.string.str_event_input);
textViewName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
textViewName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
linearLayout.addView(textViewName);
//get possible providers
final Object[] objects = PipelineBuilder.getInstance().getPossibleEventConnections(mainObject);
//
if (objects.length > 0) {
for (int i = 0; i < objects.length; i++) {
if(PipelineBuilder.getInstance().isManagedFeedback(objects[i]))
continue;
CheckBox checkBox = new CheckBox(activity);
checkBox.setText(objects[i].getClass().getSimpleName());
checkBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
Object[] providers = PipelineBuilder.getInstance().getEventConnections(mainObject);
if (providers != null) {
for (Object provider : providers) {
if (objects[i].equals(provider)) {
checkBox.setChecked(true);
break;
}
}
}
final int count = i;
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
final Object o = objects[count];
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
PipelineBuilder.getInstance().addEventConnection(mainObject, (Component) o);
} else {
PipelineBuilder.getInstance().removeEventConnection(mainObject, (Component) o);
}
}
});
linearLayout.addView(checkBox);
}
} else
{
return null;
}
tableRow.addView(linearLayout);
return tableRow;
}
/**
* @param activity Activity
* @param mainObject Object
* @param dividerTop boolean
* @return TableRow
*/
public static TableRow createModelTable(Activity activity, final Object mainObject, boolean dividerTop, int heading)
{
TableRow tableRow = new TableRow(activity);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout linearLayout = new LinearLayout(activity);
linearLayout.setOrientation(LinearLayout.VERTICAL);
if (dividerTop)
{
//add divider
linearLayout.addView(Util.addDivider(activity));
}
TextView textViewName = new TextView(activity);
textViewName.setText(heading);
textViewName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
textViewName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
linearLayout.addView(textViewName);
//get possible providers
final Object[] objects = (mainObject instanceof IModelHandler) ?
PipelineBuilder.getInstance().getAll(PipelineBuilder.Type.Model) :
PipelineBuilder.getInstance().getModelHandlers();
if (objects.length > 0) {
for (int i = 0; i < objects.length; i++) {
CheckBox checkBox = new CheckBox(activity);
checkBox.setText(objects[i].getClass().getSimpleName());
checkBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
Object[] connections = PipelineBuilder.getInstance().getModelConnections(mainObject);
if (connections != null) {
for (Object conn : connections) {
if (objects[i].equals(conn)) {
checkBox.setChecked(true);
break;
}
}
}
final int count = i;
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
final Object o = objects[count];
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
PipelineBuilder.getInstance().addModelConnection((Component) mainObject, (Component) o);
} else {
PipelineBuilder.getInstance().removeModelConnection((Component) mainObject, (Component) o);
}
}
});
linearLayout.addView(checkBox);
}
} else
{
return null;
}
tableRow.addView(linearLayout);
return tableRow;
}