2017-03-10 10 views
1

私はTextViewに下線を付けて作成しました。完全に動作していますが、TextViewの下線の高さを上げる方法を知る必要があります。私は多くをチェックしましたが、アンダーラインを描く方法のみを示していますが、下線の高さのサイズをどのように増やすかについてはここには誰もいません。アンドロイドでTextViewの下線の高さを増やす

ご利用いただけますか?

enter image description here

どのように私は、下線の高さを上げることができますか?

+0

参照この:http://stackoverflow.com/questions/19046614/how-to-underline-text-in- textview-with-some-color-that-of-text – anil

+0

私はこれを試していません、下線の色を変更しました。しかし、高さを変更しません... –

答えて

0

もう一つの方法は、あなたがViewあなたはカスタムビューを作成することができ、この

<View 
    android:layout_width="match_parent" // same as textview width 
    android:layout_height="1dp" // height that you want 
    android:background="@color/black"/> 
+0

私はこのビューのメソッドを試してみてください。肖像画をlanscape実際のテキストビューのテキストサイズにincresed線の幅.... –

+0

あなたはそれぞれ –

+0

yes.iのための別のレイアウトを設計することができます別のレイアウトのようなビューを追加するためのアプリのサイズ –

1

を試してみてください

XMLで TextViewの下を追加することができるということです。それはかなり簡単です。下線に任意の太さを付けることができます。

CustomView.java

package com.rachit.customview; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Paint.Style; 
import android.util.AttributeSet; 
import android.view.View; 

/** 
* Custom View 
* Created by Rachit on 18-Apr-16. 
*/ 
public class CustomView extends View { 

    // Label text 
    private String viewText; 
    // Underline Thickness 
    private float underlineThickness; 

    // Paint for drawing custom view 
    private Paint viewPaint; 

    public CustomView(Context context, AttributeSet attributeSet) { 
     super(context, attributeSet); 
     viewPaint = new Paint(); 
     // Get the attributes specified in attrs.xml using the name we included 
     TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.CustomView, 0, 0); 

     try { 
      // Get the text and colors specified using the names in attrs.xml 
      viewText = typedArray.getString(R.styleable.CustomView_viewText); 
      underlineThickness = typedArray.getDimension(R.styleable.CustomView_underlineThickness, 1f); 
     } finally { 
      typedArray.recycle(); 
     } 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // Draw the View 

     // Drawing the text on the view 
     viewPaint.setColor(Color.BLACK); // Set Text color to whatever you want 
     viewPaint.setTextSize(50); // Set Text Size to whatever you want 
     canvas.drawText(viewText, getX() + 5, getMeasuredHeight()/2 + 20, viewPaint); // 5 and 20 are the left and top padding, you can customize that too. 

     viewPaint.setColor(Color.BLACK); // Set Underline color to whatever you want 
     canvas.drawRect(getX(), getMeasuredHeight() - underlineThickness, getX() + getMeasuredWidth(), getMeasuredHeight(), 
       viewPaint); // Set the start and end points of the Underline 

    } 

    public String getViewText() { 
     return viewText; 
    } 

    public void setViewText(String viewText) { 
     this.viewText = viewText; 
     invalidate(); 
     requestLayout(); 
    } 

    public float getUnderlineThickness() { 
     return underlineThickness; 
    } 

    public void setUnderlineThickness(float underlineThickness) { 
     this.underlineThickness = underlineThickness; 
     invalidate(); 
     requestLayout(); 
    } 
} 

attrs.xmlres/valuesフォルダ

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="CustomView"> 

     <attr name="viewText" format="string" /> 
     <attr name="underlineThickness" format="dimension" /> 

    </declare-styleable> 
</resources> 

に今、あなたはXMLであなたのビューを定義することができますのCustomViewを作成するには

このように:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.rachit.customview.MainActivity"> 

    <Button 
     android:id="@+id/change" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Change View" /> 

    <com.rachit.customview.CustomView 
     android:id="@+id/customView" 
     android:layout_width="wrap_content" 
     android:layout_height="35dp" 
     android:layout_marginTop="30dp" 
     custom:underlineThickness="5dp" 
     custom:viewText="My View" /> 
</LinearLayout> 

そしてあなたも、プログラムでビューを制御することができます。

MainActivity.java

package com.rachit.customview; 

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 

import java.util.Random; 

public class MainActivity extends AppCompatActivity { 

    CustomView customView; 
    Button change; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     customView = (CustomView) findViewById(R.id.customView); 
     change = (Button) findViewById(R.id.change); 

     change.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       customView.setViewText("Wassup??"); 
       customView.setUnderlineThickness(1.0f); 
      } 
     }); 
    } 
} 

出力は次のようになります。

Custom View

し、ボタンをクリックすることで、ビューをプログラムで変更することができます。

Programmatically Modified

+0

ちょっと数秒チェック&返信... –

+0

それは働いていますが、別の問題に直面しています。このメソッドを使用すると下線が全体のテキストビュー幅になります。しかし、私は下のテキストのみでアンダーラインを設定したいと思います。余分なスペースはありませんか?ここには何か方法がありますか? –

関連する問題