[英]Align TextViews to top of LinearLayout
我正在創建一個在LinearLayout中具有四個不同TextView的應用程序。 LinearLayout的權重總和為4,每個TextView的權重為1(以使其具有均勻的寬度)。 我的第一個問題是,兩個TextView的高度都比其他兩個高,這使它們居中,但是我希望它們在頂部與LinearLayout對齊,以便所有TextView都具有一條直線。
我的第二個問題是,其中一個字符串比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);
發布xml代碼...但是在我看來,您沒有將高度或寬度設置為0dp(如果是垂直布局則為高度,如果是水平則為寬度)...此外,請確保所有textViews都具有android :singleLine =“ true”屬性
由於每個用戶僅需要根據他們所屬的組查看這些TextView之一,因此我將其設置為用戶設置,並僅顯示該用戶的正確TextView。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.