65

XML以下をレイアウトファイルに含めると、下の画像が表示されます。よく見ると、TextView上下にスペースがあることがわかります。

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E1"
android:background="#ff00ff00"/>

ここに画像の説明を入力

スペースを削除したい。それを取り除く方法は?それはなんと呼ばれていますか?誰かが手がかりを持っているなら..私に知らせてください。前もって感謝します。

4

14 に答える 14

98

android:includeFontPadding="false"それが役立つかどうかを確認してください。私の経験では、少しは役に立ちますが、TextView の寸法を正確なピクセル単位のテキスト サイズに縮小する方法はありません。

より良い結果が得られる場合とそうでない場合がある唯一の代替手段は、少しごまかして、たとえば高さの"24sp"代わりにテキストサイズに一致するように寸法を配線することです。"wrap_content"

于 2011-07-06T09:37:36.573 に答える
28

私も同じ問題を抱えていました。属性android:includeFontPadding="false"が機能しません。私はこの問題を次のように解決しました:

public class TextViewWithoutPaddings extends TextView {

    private final Paint mPaint = new Paint();

    private final Rect mBounds = new Rect();

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

    public TextViewWithoutPaddings(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextViewWithoutPaddings(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        final String text = calculateTextParams();

        final int left = mBounds.left;
        final int bottom = mBounds.bottom;
        mBounds.offset(-mBounds.left, -mBounds.top);
        mPaint.setAntiAlias(true);
        mPaint.setColor(getCurrentTextColor());
        canvas.drawText(text, -left, mBounds.bottom - bottom, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        calculateTextParams();
        setMeasuredDimension(mBounds.width() + 1, -mBounds.top + 1);
    }

    private String calculateTextParams() {
        final String text = getText().toString();
        final int textLength = text.length();
        mPaint.setTextSize(getTextSize());
        mPaint.getTextBounds(text, 0, textLength, mBounds);
        if (textLength == 0) {
            mBounds.right = mBounds.left;
        }
        return text;
    }
}
于 2015-09-29T05:49:34.713 に答える
24

android:includeFontPadding="false"かなり良いですが、正確には取得できません。境界線の精度が必要な場合があるため、負のマージンを適用して自分で把握できます。

下マージンと上マージンを負の値に設定してみてください。

このようなもの:

android:layout_marginTop="-5dp"
android:layout_marginBottom="-5dp"

それに応じて値を調整します。

于 2014-08-25T20:09:35.027 に答える
3

DynamicMindの回答に追加したかったのは、 TextViews の周りにスペースが表示される理由は、デフォルトで使用される9 パッチの背景にパディングされているためです。

9 パッチ テクノロジを使用すると、効果的にパディングされるコンテンツ領域を指定できます。ビューのパディングを明示的に設定しない限り、そのパディングが使用されます。たとえば、パディングが設定されているビューに 9 パッチの背景をプログラムで設定すると、パディングがオーバーライドされます。逆に、パディングを設定すると、9 パッチ バックグラウンドで設定された内容が上書きされます。

残念ながら、XML レイアウトでは、これらの操作の順序を決定することはできません。TextViews から背景を削除するだけで役立つと思います。

android:background="@null"
于 2011-07-06T09:44:35.370 に答える
2
public class TopAlignedTextView extends TextView {

    public TopAlignedTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopAlignedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        setIncludeFontPadding(false); //remove the font padding
        setGravity(getGravity() | Gravity.TOP);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        TextPaint textPaint = getPaint();
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();
        canvas.save();

        //remove extra font padding
        int yOffset = getHeight() - getBaseline();
        canvas.translate(0, - yOffset / 2);

        if (getLayout() != null) {
            getLayout().draw(canvas);
        }
        canvas.restore();
    }
}
于 2015-06-10T17:21:35.637 に答える
1

レイアウトマージンを定義しましたか? 例えば:

android:layout_marginTop="5dp"

それ以外の場合、テキスト ビューが LinearLayout または他のコンテナー内にラップされている場合、そのコールドにもパディングまたはマージンがあります。

于 2011-07-06T09:06:18.537 に答える
1

この回答を少し変更して、kotlin クラスを使用して拡張AppCompatTextViewし、垂直方向のパディングをトリミングしました。

設定が可能ですandroid:fontFamily。メソッドはパフォーマンスのためcalculateTextParams()に から移動されonDraw()ました。複数行のテキストについてはテストされていません:

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView

class NoPaddingTextView : AppCompatTextView
{
  private val boundsRect = Rect()
  private val textParams = calculateTextParams()

  constructor(context : Context?)
  : super(context)

  constructor(context : Context?, attrs : AttributeSet?)
  : super(context, attrs)

  constructor(context : Context?, attrs : AttributeSet?, defStyleAttr : Int)
  : super(context, attrs, defStyleAttr)

  override fun onDraw(canvas : Canvas)
  {
    with(boundsRect) {
      paint.isAntiAlias = true
      paint.color = currentTextColor
      canvas.drawText(textParams,
                      -left.toFloat(),
                      (-top - bottom).toFloat(),
                      paint)
    }
  }

  override fun onMeasure(widthMeasureSpec : Int, heightMeasureSpec : Int)
  {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    calculateTextParams()
    setMeasuredDimension(boundsRect.width() + 1, -boundsRect.top + 1)
  }

  private fun calculateTextParams() : String
  {
    return text.toString()
    .also {text ->
      text.length.let {textLength ->
        paint.textSize = textSize
        paint.getTextBounds(text, 0, textLength, boundsRect)
        if(textLength == 0) boundsRect.right = boundsRect.left
      }
    }
  }
}
于 2019-12-01T17:45:48.017 に答える
0
android:background="@android:drawable/editbox_background"

editbox_background を必要に応じて変更して使用します。Androidは上記のコードのようなバックグラウンドでいくつかのビルドを提供するため、要件に応じて選択してください。それはあなたの助けになるかもしれません。

于 2011-07-06T09:22:47.267 に答える
0

LinearLayout 内では、デフォルトのパディングが問題になる場合があります。0dpに設定してみてください。それは私のために働いた。

于 2013-03-27T13:51:33.230 に答える
0

TopAlignedTextView コードの答え: TopAlignedTextView@GitHub

レイアウトで使用します。

<com.github.captain_miao.view.TopAlignedTextView
    android:id="@+id/text_a"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:text="@string/text_demo_a"
/>

ここに画像の説明を入力

于 2016-10-16T16:25:36.090 に答える