0

我创建了一个应用程序,它具有LinearLayout中的四个不同的TextView。 LinearLayout的权重总和为4,每个TextView的权重为1(使它们的宽度均匀)。 我的第一个问题是两个TextView的高度比另外两个高,这使它们居中,但我希望它们与LinearLayout顶部对齐,因此所有的TextView都有一条直线。将TextViews对齐到LinearLayout的顶部

我的第二个问题是其中一个字符串比TextView的宽度更长,这使得TextView稍微向下(它甚至不再居中,并且在LinearLayout父类之外,因此所有的TextView宽度没有显示)。

我的代码:

 LinearLayout layout = new LinearLayout(context); 
     LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     layout.setLayoutParams(layoutParams); 
     layout.setWeightSum(lessons); 

     for (int i = 0; i < lessons; i++) { 
      TextView text = new TextView(context); 
      LayoutParams textParams = new LayoutParams((int) (0 * (context.getResources().getDisplayMetrics().density) + 0.5f), 
        (int) (length[i] * (context.getResources().getDisplayMetrics().density) + 0.5f), 1); 
      text.setLayoutParams(textParams); 

      System.out.println(length[i]); 

      text.setPadding((int) (8 * (context.getResources().getDisplayMetrics().density) + 0.5f), 0, 0, 0); 
      text.setTextColor(context.getResources().getColor(R.color.white_text)); 
      text.setBackgroundColor(context.getResources().getColor(R.color.class.getField(name[i]).getInt(null))); 
      text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); 
      text.setGravity(Gravity.CENTER_VERTICAL); 
      text.setText(R.string.class.getField(name[i]).getInt(null)); 

      layout.addView(text); 
     } 

     week[day].addView(layout); 
+0

2建议.... 1-)移动到RelativeLayout,2-)使用xml布局代替 –

+0

@Muhannad Fakhouri我将测试RelativeLayout,但是xm不是一个选择,因为布局需要基于动态在用户选择上。 – WeeRox

回答

0

由于每个用户只需要根据属于哪个组来查看其中一个TextView,我会将其设置为用户设置并只显示该用户的正确TextView。

0

发布的XML代码...但在我看来,你没有设置高度或宽度0dp(高度如果它是一个垂直布局/宽度如果水平) ...另外,请确保所有textView都具有android:singleLine =“true”属性