2011-07-06 146 views
55

当我包括以下XML布局文件,我可以看到下面的图像。如果你看到它,你会意识到TextView有顶部和底部空间。如何删除Android的文本视图上的顶部和底部空间

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

enter image description here

我要删除的空间。如何删除它?这叫什么? 如果有人有线索,请让我知道。提前致谢。

+0

[Android:TextView:删除顶部和底部的间距和填充]可能的重复(http://stackoverflow.com/questions/4768738/android-textview-remove-spacing-and-padding-on-top-and -bottom) –

回答

1

您是否定义了布局边距? 例如:

android:layout_marginTop="5dp" 

否则,如果您的文本视图上缠绕有LinearLayout中或其他容器内,然后那个寒冷要么填充或裕太。

0
android:background="@android:drawable/editbox_background" 

使用它根据你改变它,你想editbox_background。 ,因为android提供了一些像上面代码一样的背景代码根据您的要求选择。 可能是对你有帮助。

+0

它不起作用 – Tim

75

尝试android:includeFontPadding="false"看看是否有帮助。根据我的经验,这会有所帮助,但是没有办法将TextView尺寸缩小到完全像素完美的文字尺寸。

唯一可能或不可能给出更好结果的替代方法是欺骗一些并硬连线尺寸以匹配文本大小,例如, "24sp"而不是"wrap_content"为高度。

+1

第一个选项+1 ... –

+4

第二个+1!请注意,我还必须包含includeFontPadding =“false”,否则文本会被裁剪一点。 – Enrichman

+5

它不适合我(Android 6.0) – Tim

3

只想添加到DynamicMind's answer这就是为什么你看到周围的TextViews间距的原因是填充在9补丁背景,他们默认使用

9修补技术允许您指定一个内容区域这实际上是填充。除非您明确设置视图的填充,否则将使用该填充。例如,当你以编程方式将9补丁背景设置为设置了填充的视图时,它们被覆盖。反之亦然,如果你设置了填充,它们会覆盖9-补丁背景设置的内容。

不幸的是,在XML布局中,不可能确定这些操作的顺序。我认为从你的TextView中删除背景会有所帮助:

android:background="@null" 
+6

对我不起作用:( – vault

+0

这解释了为什么我有填充我没有在我的xml中设置。必须设置padding = 5dp来覆盖默认填充表格9补丁背景。 谢谢 –

+0

它不起作用 – Tim

0

在LinearLayout中,默认的填充可能是一个问题。尝试将其设置为0dp。它为我工作。

4

我面临同样的问题。 这是一个很好的答案:How to align the text to top of TextView?

但是代码有点未完成,不支持所有字体大小。行

int additionalPadding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics()); 

更改为

int additionalPadding = getTextSize() - getLineHeight(); 

完整的C#代码(单声道)移除顶偏:

public class TextControl : TextView { 
    public TextControl (Context context) : base (context) 
    { 
     SetIncludeFontPadding (false); 
     Gravity = GravityFlags.Top; 
    } 

    protected override void OnDraw (Android.Graphics.Canvas canvas) 
    { 
     if (base.Layout == null) 
      return; 

     Paint.Color = new Android.Graphics.Color (CurrentTextColor); 
     Paint.DrawableState = GetDrawableState(); 

     canvas.Save(); 

     var offset = TextSize - LineHeight; 
     canvas.Translate (0, offset); 

     base.Layout.Draw (canvas); 

     canvas.Restore(); 
    } 
} 
+0

例如引用翻译在最后一刻的翻译量,这段代码没有。解决方法是正确的,否则。 –

+0

我已经尝试了很多hacks,包括引用的例子,定义了任意的'TypedValue.COMPLEX_UNIT_DIP,5'。最后,这是唯一的工作是为了对齐不同textSizes的2个TextView,没有任何幻数。我会给你+10,如果我可以:) –

18

尝试设置您的底部和顶部的利润为负。

是这样的:

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

因此调整值。

+0

帮助我thanxxx –

+0

伟大和简单:D –

+0

这应该是正确的答案 – Aggressor

4

这是节省我们一天的代码。它是用单C#代码maksimko改编:

public class TopAlignedTextView extends TextView { 

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

    /*This is where the magic happens*/ 
    @Override 
    protected void onDraw(Canvas canvas){ 

     float offset = getTextSize() - getLineHeight(); 
     canvas.translate(0, offset); 
     super.onDraw(canvas); 
    } 
} 

仍然有玩弄textView.setIncludeFontPadding(false)因为我们用不同的字体大小排列TextViews

+0

这种看法导致我几次崩溃! –

+1

小心地显示堆栈跟踪,而不是向许多用户投票回答一个有用的答案? –

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(); 
    } 
} 
16

我有同样的问题。属性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; 
    } 
} 
+1

伟大的解决方案!尝试不同的文字大小,像魅力:) – dakshbhatt21

+0

更新:在上面的代码中,当我使用q,g,y,p小写时,他们从底部切下。所以对于'setMeasuredDimension(mBounds.width()+ 1,-mBounds.top + 1);''setMeasuredDimension(mBounds.width()+ 1,-mBounds.top + mBounds.bottom);' – dakshbhatt21

+1

@ dakshbhatt21很好的 – qinmiao

0

的TopAlignedTextView代码answer[email protected]

布局使用它:

<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" 
/> 

enter image description here

0

我的方式固定这是非常哈克的,但我设法让文本坐在我想要的位置,将文本视图的高度设置为静态并一直摆弄它直到它只是几乎不适合文字。在我的情况下,我使用的字体样式的高度为64sp,因此我将textview的高度设置为50sp,并且它的工作正常。我还必须将foreground_gravity设置为底部。

相关问题