Android LinearLayout以编程方式向左移一列,向右浮动一列

时间:2018-12-10 21:32:13

标签: android android-layout

我的Android布局在LinearLayout中包含2个TextView。但是,我希望第二个文本视图正确对齐。

说明:

enter image description here

// LinearLayout A
LinearLayout linearLayoutA = new LinearLayout(this);
linearLayoutA.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayoutWorkoutPlansSessionsMain.addView(linearLayoutA);

// Title
TextView textViewExerciseTitle = new TextView(this);
textViewExerciseTitle.setText("Pull ups");
textViewExerciseTitle.setTextSize(16);
textViewExerciseTitle.setTextColor(Color.BLACK);
linearLayoutA.addView(textViewExerciseTitle);

// Reps & sets
TextView textViewSetsReps = new TextView(this);
textViewSetsReps.setText("8 x 4");
textViewSetsReps.setTextSize(16);
textViewSetsReps.setTextColor(Color.BLACK);
textViewSetsReps.setPadding(20,0,0,0);
linearLayoutA.addView(textViewSetsReps);

1 个答案:

答案 0 :(得分:2)

TextView textViewSetsReps = new TextView(this);
    textViewSetsReps.setText("8 x 4");
    textViewSetsReps.setTextSize(16);
    textViewSetsReps.setTextColor(Color.BLACK);
    textViewSetsReps.setPadding(20,0,0,0);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,  1.0f);
    textViewSetsReps.setLayoutParams(params);
    textViewSetsReps.setGravity(Gravity.RIGHT);
    ll.addView(textViewSetsReps);

您可以像这样编辑第二个TextView!