簡體   English   中英

如何刪除TextView上邊距?

[英]How to remove TextView top margin?

我確實遇到TextView問題。 我不希望它上面有任何邊距/填充。

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/ptp_hour"
          android:textColor="@color/black"
          android:textSize="100dp"
          android:height="100dp"
          android:layout_marginTop="0dp"
          android:paddingTop="0dp"
          android:includeFontPadding="false"
          android:maxLines="1"
          android:text="10"/>

我的TextView看起來像這樣,盡管textSizeheight設置為相同的值,但字體上方還有一個空格。 這讓我感到困擾,因為我想把另一個視圖放在相對於字體的頂部。 這個間距是否包含在字體本身中?

RelativeLayout中的TextView

還有一個問題:如果我發現距離頂部20dp和底部7dp的余量在我的設備上完美運行,我可以依賴它在其他屏幕上的表現方式相似嗎? (這些邊距適用於按鈕)

使用android:includeFontPadding="false"在類似的情況下幫助了我很多。

我有同樣的問題,設置android:includeFontPadding=false沒有幫助。 我能在合理的時間內找到的最佳解決方案是覆蓋TextView的onDraw方法,並調整畫布以獲取字體度量的top和ascent值之間的差異:

FontMetricsInt fontMetricsInt;
@Override
protected void onDraw(Canvas canvas) {
    if (adjustTopForAscent){
        if (fontMetricsInt == null){
            fontMetricsInt = new FontMetricsInt();
            getPaint().getFontMetricsInt(fontMetricsInt);
        }
        canvas.translate(0, fontMetricsInt.top - fontMetricsInt.ascent);
    }
    super.onDraw(canvas);
}

你需要做的是將另一個視圖放在相對於字體頂部的位置,並在dip中給它一個負面的android:layout_marginBottom ,使其與字體的頂部相匹配。 如果字體有邊距,我認為沒有更好的方法。

是的,默認包含此空格。 您無法根據我的搜索區域刪除該空間。 所以你需要實現一些邏輯來擁有這樣的視圖。

見下圖:

在此輸入圖像描述

它不是一個好主意,但你可以像下面的代碼:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:background="#ffffff">
    <TextView android:layout_width="wrap_content"           
        android:layout_height="wrap_content"           
        android:id="@+id/ptp_hour"           
        android:textColor="@android:color/black"           
        android:textSize="100sp"
        android:lineSpacingMultiplier="0.8"
        android:scrollY="-100dp"
        android:scrollX="100dp"
        android:text="10"/>
    <TextView 
        android:text="10"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textColor="#00FFFFFF"
        android:shadowColor="#000000"
        android:shadowDy="-50"
        android:shadowRadius="1"
        android:textSize="100dp"/>

</LinearLayout>

在這里,第一個“10”是你的屬性,第二個是我為你設置的。

請享用。 :))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM