2013-10-17 84 views
0

我正在製作一個android應用程序,我想知道如何在android中使用下劃線實現彩色標題。下面的圖片是我想要的。添加帶有下劃線的彩色文本視圖

enter image description here

到目前爲止,我已經寫代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".AddContact" > 

    <TextView 
     android:id="@+id/lbl_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:text="@string/heading_name" /> 

    <LinearLayout 
     android:layout_width="337dp" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="6" > 

     <EditText 
      android:id="@+id/edit_first_name" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_weight="5" 
      android:hint="@string/hint_firstname" 
      android:inputType="textCapSentences" /> 

     <ImageButton 
      android:id="@+id/addimage" 
      android:layout_width="0dp" 
      android:layout_height="58dp" 
      android:layout_weight="1" 
      android:background="@drawable/ic_social_person" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="337dp" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="5" > 

     <EditText 
      android:id="@+id/edit_last_name" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_weight="4" 
      android:hint="@string/hint_lastname" 
      android:inputType="textCapSentences" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/lbl_number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/heading_number" /> 

    <LinearLayout 
     android:layout_width="337dp" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <EditText 
      android:id="@+id/edit_number" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_weight="2" 
      android:hint="@string/hint_number" 
      android:inputType="number" /> 

     <Spinner 
      android:id="@+id/contact_number_spinner" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/lbl_email" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/heading_email" /> 

    <LinearLayout 
     android:layout_width="337dp" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <EditText 
      android:id="@+id/edit_email" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_weight="2" 
      android:hint="@string/hint_email" 
      android:inputType="textEmailAddress" /> 

     <Spinner 
      android:id="@+id/contact_email_spinner" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/lbl_address" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/heading_address" /> 

    <LinearLayout 
     android:layout_width="337dp" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <EditText 
      android:id="@+id/edit_address" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_weight="2" 
      android:hint="@string/hint_address" 
      android:inputType="textPostalAddress" /> 

     <Spinner 
      android:id="@+id/contact_address_spinner" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/lbl_event" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/heading_event" /> 

    <LinearLayout 
     android:layout_width="337dp" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <EditText 
      android:id="@+id/edit_date" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_weight="2" 
      android:hint="@string/hint_date" 
      android:inputType="date" /> 

     <Spinner 
      android:id="@+id/contact_date_spinner" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</LinearLayout> 

任何幫助,將不勝感激

+0

請參考以下鏈接http://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android/3282962#3282962 – SathishKumar

回答

1

你可以實現一個簡單的彩色視圖這樣一行:

<View 
    android:id="@+id/view1" 
    android:layout_width="wrap_content" 
    android:layout_height="1dip" 
    android:background="your color" /> 
+0

非常感謝你的幫助:) – user123456

1

對於下劃線的文本字符串中的資源文件添加標籤的文本這樣的..

<resource> 
<string name="my_text"><u>This is an underline</u>.</string> 

併爲您的TextView改變文字顏色添加此標記設置你的顏色..

android:textColor="@android:color/holo_orange_light" 
+0

謝謝你這麼多。我真的很感激它:) – user123456

0

試試這個:

TextView textView = (TextView)findViewById(R.id.textview); 
SpannableString content = new SpannableString("Content"); 
content.setSpan(new UnderlineSpan(), 0, content.length(), 0); 
textView.setText(content); 
textview.settextcolor(Color.Red); 

這會幫助你。

0

你可以使用String資源來做到這一點。 它支持像<b></b>, <i></i> and <u></u>. HTML標籤如果你想強調的文本使用此

textview.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); 

否則,

在你的資源文件夾使用此代碼

<resource> 
<string name="your_string_here">This is an &lt;u>underline&lt;/u>.</string> 

並在代碼中使用本。

TextView textView = (TextView) view.findViewById(R.id.textview); 
textView.setText(Html.fromHtml(String.format(getString(R.id.textview), ...)));