【问题标题】:Setting TextView to right of vertical LinearLayout in android在android中将TextView设置为垂直LinearLayout的右侧
【发布时间】:2011-12-18 05:13:23
【问题描述】:

我尝试将 TextView 添加到垂直 LinearLayout 并将文本视图与布局右侧对齐:

LinearLayout temprLayout=new LinearLayout(this);
            temprLayout.setOrientation(LinearLayout.VERTICAL);

            theTemprature = new TextView(this);
            theTemprature.setVisibility(View.VISIBLE);
            theTemprature.setTextSize(21);
            theTemprature.setTextColor(0xffffCC33);

            theUVText = new TextView(this);
            theUVText.setVisibility(View.VISIBLE);
            theUVText.setTextSize(21);
            theUVText.setTextColor(0xfff5b800);
            theUVText.setBackgroundColor(0xff423234);
            theUVText.setGravity(Gravity.RIGHT | Gravity.TOP);

            theUVText.setLayoutParams(new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                    Gravity.RIGHT));

            temprLayout.addView(theTemprature, new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                    Gravity.RIGHT));
            temprLayout.addView(theUVText, new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                    Gravity.RIGHT));

但文本视图仍然与布局的左侧对齐

【问题讨论】:

    标签: android layout textview android-linearlayout


    【解决方案1】:

    尝试将您的 LinearLayout 设置为 MATCH_PARENT 宽度。

    【讨论】:

    • FILL_PARENT 已被 android-8 弃用,对 android-8 及更高版本使用 MATCH_PARENT。
    • 有趣,出于某种原因,我认为情况正好相反。感谢@Renaud 的提示
    • 重力权只能走到包含布局的最右边。如果您希望它一直正确,您必须确保 LinearLayout 填充该空间—​​—希望这是有道理的!
    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多