28

私が持っているのはTextView、シェイプサークルを作成し、TextView使用したさまざまな条件に基づいてさまざまな背景色を設定することです。さまざまな条件に基づいて背景色を設定することはできますが、TextView図形を円にすることはできません。それで、それをどのように行うことができますか。これを解決するのを手伝ってください。

私が使用したコード:

TextView txt_stage_display = (TextView)findViewById(R.id.txt_stage_display);

if(m_enStage[position] == enSTAGE_START)
{
    txt_stage_display.setBackgroundColor(Color.parseColor("#D48393"));
}
else if(m_enStage[position] == enSTAGE_FLOW)
{
    txt_stage_display.setBackgroundColor(Color.parseColor("#D48393"));
}
    else if(m_enStage[position] == enSTAGE_SAFE)
{
    txt_stage_display.setBackgroundColor(Color.parseColor("#66B0CC"));
}
else if(m_enStage[position] == enSTAGE_UNSAFE)
{
    txt_stage_display.setBackgroundColor(Color.parseColor("#D8C627"));
}
else if(m_enStage[position] == enSTAGE_FERTILE)
{
    txt_stage_display.setBackgroundColor(Color.parseColor("#67A05E"));
}
else
{
    txt_stage_display.setBackgroundColor(Color.parseColor("#808080"));
}
4

5 に答える 5

47

色の量が比較的少ない場合は、色ごとに描画可能なファイルを作成できます。たとえば、ファイルbg_red.xmlを作成します。

<?xml version="1.0" encoding="utf-8"?>
<item xmlns:android="http://schemas.android.com/apk/res/android">
  <shape>
      <solid android:color="#f00" />
      <corners
          android:topLeftRadius="30dp"
          android:topRightRadius="30dp"
          android:bottomLeftRadius="30dp"
          android:bottomRightRadius="30dp"
          />
  </shape>
</item>

次に、TextViewの定義を割り当てます。

<TextView 
    android:id="@+id/tv"
    android:layout_height="60dp"
    android:layout_width="60dp" 
    android:text="X" 
    android:textColor="#fff"
    android:textSize="20sp"
    android:background="@drawable/bg_red"
    android:gravity="center_vertical|center_horizontal" 
    />

幅は背景の角の半径の2倍であることに注意してください。

コードから色を変更するには:

TextView v = (TextView) findViewById(R.id.my_text_view);
v.setBackgroundResource(R.drawable.bg_blue);
于 2012-04-25T13:47:27.610 に答える
24

受け入れられた答えに追加するには、図形の内側にサイズタグを追加し、高さと幅が十分に大きいことを確認すると、textViewに多くの文字が含まれている場合でも、背景が円になります。

<shape>
 <solid android:color="#f00" />
 <corners
    android:topLeftRadius="30dp"
   android:topRightRadius="30dp"
     android:bottomLeftRadius="30dp"
     android:bottomRightRadius="30dp"
     />
 <size 
  android:height="25dp"
   android:width="25dp"/>
</shape>
于 2014-07-16T10:33:41.323 に答える
6
TextView textView = (TextView) findViewById(R.id.my_text_view);             
Drawable drawable = textView.getBackground();
drawable.setColorFilter(getResources().getColor(color), PorterDuff.Mode.SRC_IN);

私のために働く

于 2017-01-15T14:12:01.887 に答える
1

Circular_hired.xmlファイルをドローアブルに追加します。

  <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >


            <solid android:color="@color/map_hide_bg"/>
            <stroke android:width="1dip" android:color="@color/map_hide_bg" />

    </shape>

ただし、レイアウトデザインのxmlファイルでは、textviewの幅と高さを修正する必要があります。円形になります。

<TextView
            android:id="@+id/tvMessages"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:background="@drawable/circular_message"
            android:padding="4dp"
            android:text="5"
            android:textColor="@color/white_color"
            android:textSize="@dimen/txt12"
            android:visibility="visible"
             />

私はそれがあなたを助けると確信しています、

于 2016-01-20T14:51:50.730 に答える
0

このように描画可能な背景に色を設定することでもこれを実現できます

TextView textView = (TextView) findViewById(R.id.my_text_view);
((GradientDrawable)textView.getBackground()).setColor(R.color.my_color);
于 2017-12-27T22:26:52.020 に答える