7

アプリでフォントのスタイルをいくつか作成しました。

これらのスタイルをビューに追加するにはどうすればよいですか?-1)スタイル属性を使用します。2)textAppearanceを使用します。

ビューには他の属性(マージン、パディング、最小幅など-ビューに2つのスタイルを指定できない)がある可能性があるため、スタイルを使用することはできません。したがって、テキスト関連の属性を個別に指定する必要がありますが、android:textColorここでは機能しません。

styles.xml:

<style name="TextAppearance.baseText" parent="@android:style/TextAppearance">
    <item name="android:textAppearance">?android:textAppearanceSmall</item>
    <item name="android:typeface">sans</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">15sp</item> 
</style>

layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="65dp"
      android:orientation="horizontal" >

      <Button
           android:id="@+id/backButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/sometext"
           android:textAppearance="@style/TextAppearance.baseText"/>
</RelativeLayout>

なぜそれが機能しないのですか?textappearanceでtextcolorを指定するにはどうすればよいですか?

4

3 に答える 3

13

Oderikがコメントで述べたように、ボタンビューにはそのtextColor属性のデフォルト値があります。この値は、属性textColorを介して設定された値をオーバーライドします。textAppearanceしたがって、2つのオプションがあります。

  • 次のスタイルでtextColorを@nullに設定します。

    <style name="Application.Button">   
      <item name="android:textAppearance">@style/Application.Text.Medium.White</item>
      <item name="android:textColor">@null</item> 
    </style>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Application.Button" />
    
  • ButtonXMLでtextColorを@nullに設定します。

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Application.Button"   
    android:textColor="@null" />
    
于 2014-01-14T00:46:35.307 に答える
8

できます。テキストの色は白です。デフォルトの色と同じです。のような他の色をそこに置く<item name="android:textColor">#AA6699</item>と、違いがわかります。2つ目は、テキストの外観をテキストの外観に設定しようとしていることです。これは理にかなっています。小文字を設定する場合は、親parent="@android:style/TextAppearance.Smallを使用してこれを行い、行を削除<item name="android:textAppearance">?android:textAppearanceSmall</item> します。3つ目は、小文字を追加してtextSizeを追加することです。意味がありません。これを試してみてください、私のために働きます:

<style name="BaseText" parent="@android:style/TextAppearance.Small">
        <item name="android:typeface">sans</item>
        <item name="android:textColor">#AA7755</item>
  </style>

すべてをスタイリッシュに設定したい場合:

<style name="BaseText" parent="@android:style/TextAppearance.Large">
        <item name="android:typeface">sans</item>
        <item name="android:textColor">#AA7755</item>
    </style>

   <style name="BaseText.Margins">
       <item name="android:layout_margin">10dip</item>
   </style>

BaseText.MarginsがBaseTextから継承する場所。次に、次を使用できます。

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        style="@style/BaseText.Margins" />
于 2012-06-27T12:49:25.950 に答える
7

を設定するandroid:TextAppearance代わりにを使用する場合は、を設定する必要があります。style:android:textColorandroid:textColor=@nullTextView

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium.MySubheader"
    android:textColor="@null"
    tools:text="Section" />

その後、それはあなたから正しく継承されますandroid:TextAppearance

<style name="TextAppearance.AppCompat.Medium.MySubheader">
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/black_54</item>
</style>
于 2015-09-18T14:17:05.300 に答える