2011-07-06 126 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設置爲底部。

相關問題