Android - 为什么 TextView 显示为有高度?

发布于 2024-12-16 13:11:38 字数 762 浏览 3 评论 0原文

我这里有一个奇怪的问题。基本上我有一个没有默认设置文本的 TextView。我本来期望它的高度为 0,因为它没有内容,但它上面和下面的元素之间似乎有一个间隙。如果我在 XML 中将高度设置为 0,然后尝试通过 Java 代码更改它,那么它不会重置高度。

如果内容为空,如何将高度设置为 0,然后允许以编程方式更改它?

这是我的代码:

<TextView 
    android:gravity="center_horizontal|center_vertical"
    android:id="@+id/connectionStatus"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:textSize="18px"
    android:textStyle="bold">
</TextView>

Java 代码是这样的:

    private void getConnectionStatus()
{
    if (hasConnection() == true)
    {
        //do something
    }
    else
    {
        connectionStatus.setHeight(48);
        connectionStatus.setText("No Internet Access");
    }   
}

I've got an odd problem here. Basically I have a TextView with no default set text. I would've expected it to have a height of 0 since it hsas no content but there seems to be a gap between the elements above and below it. If I set the height to 0 in the XML and then try and change it through Java code then it does not reset the height.

How do I set the height to be 0 if the content is blank but then allow it to be changed programmatically?

Here is the code that I have:

<TextView 
    android:gravity="center_horizontal|center_vertical"
    android:id="@+id/connectionStatus"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:textSize="18px"
    android:textStyle="bold">
</TextView>

and the Java code is this:

    private void getConnectionStatus()
{
    if (hasConnection() == true)
    {
        //do something
    }
    else
    {
        connectionStatus.setHeight(48);
        connectionStatus.setText("No Internet Access");
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乄_柒ぐ汐 2024-12-23 13:11:39

使用 xml 布局内部“消失”的可见性。然后在Java代码中调用connectionStatus.setVisibility(View.VISIBLE);

Use visibility "gone" inside of the xml layout. Then in the Java code call connectionStatus.setVisibility(View.VISIBLE);

_蜘蛛 2024-12-23 13:11:39

即使组件没有内容,它们仍可能会显示自身。例如,可以显示边框或其可视区域。为了使其根本不显示,您需要使用setVisibility(View.GONE)

Components may still display themselves even if they don't have content. For example, the may display a border or their viewable area. In order to make it not show up at all you need to use setVisibility(View.GONE).

森林很绿却致人迷途 2024-12-23 13:11:39

我经常想知道这种行为是否直观。如果您想要一个在文本为空时没有高度的 TextView ,您可以制作一个:

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;

public class NoHeightWhenEmptyTextView extends android.support.v7.widget.AppCompatTextView {

    public NoHeightWhenEmptyTextView(Context context) {
        super(context);
    }

    public NoHeightWhenEmptyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NoHeightWhenEmptyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int newHeightMeasureSpec = heightMeasureSpec;
        if (TextUtils.isEmpty(getText())) {
            newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, newHeightMeasureSpec);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        // ConstraintLayout totally ignores the new measured height after non-empty text is set.
        // A second call to requestLayout appears to work around the problem :(
        requestLayout();
    }
}

I've often wondered if this behaviour is intuitive. If you want a TextView that has no height when the text is empty you can make one:

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;

public class NoHeightWhenEmptyTextView extends android.support.v7.widget.AppCompatTextView {

    public NoHeightWhenEmptyTextView(Context context) {
        super(context);
    }

    public NoHeightWhenEmptyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NoHeightWhenEmptyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int newHeightMeasureSpec = heightMeasureSpec;
        if (TextUtils.isEmpty(getText())) {
            newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, newHeightMeasureSpec);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        // ConstraintLayout totally ignores the new measured height after non-empty text is set.
        // A second call to requestLayout appears to work around the problem :(
        requestLayout();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文