2017-05-19 39 views
0

我有兩個ListViews(leftList,rightList)。我也有一個TextView,我用它們作爲行視圖。 我有一個矩形可繪製的形狀,並將其設置爲TextView的背景。android - 如何以編程方式設置圓角半徑?

我想改變這種形狀,左邊有圓角。

我試了一下:

 GradientDrawable gradientDrawable = new GradientDrawable(); 
     // gradientDrawable.setCornerRadius(30); 
     ((GradientDrawable)gradientDrawable.mutate()).setCornerRadius(30); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      viewHolderPattern.digits.setBackground(gradientDrawable); 
     } 

我已經創造了一個繪製新的佈局設置,並設置與setBackgroundRescource TextView的,但仍然沒有工作右上角半徑。

,我在這兩個列表視圖,爲項目使用的TextView

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/digitsTextView" 
    android:textSize="15dp" 
    android:paddingTop="7dp" 
    android:paddingBottom="8dp" 
    android:fontFamily="monospace" 
    android:textColor="@color/selected_items" 
    android:background="@drawable/digital_text_shape"> 
</TextView> 

形狀佈局digital_text_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <stroke 
     android:width="1dp" 
     android:color="@color/orange" /> 
    <solid android:color="@color/orange" /> 
    <corners 
     android:bottomLeftRadius="20dp" 
     android:bottomRightRadius="0dp" 
     android:topLeftRadius="20dp" 
     android:topRightRadius="0dp" 
     /> 
    <padding 
     android:bottom="0dp" 
     android:left="20dp" 
     android:right="0dp" 
     android:top="0dp" /> 
</shape> 

左邊列表和右邊的列表

<!-- Left ListView --> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        > 
         <ListView 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:layout_gravity="center_horizontal" 
          android:id="@+id/left_listView" 
          android:divider="@android:color/transparent" 
          android:dividerHeight="0.1sp" 
          android:choiceMode="singleChoice" 

          > 
         </ListView> 
       </LinearLayout> 

       <!-- Right ListView --> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        > 
         <ListView 
          android:id="@+id/right_listView" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:divider="@android:color/transparent" 
          android:dividerHeight="0.1sp" 
          android:choiceMode="singleChoice" 
          > 
         </ListView> 
       </LinearLayout> 
+0

你知道,TextViews你可以clsoe他們/>。除非裏面有東西,否則你不需要第二個標籤。 – Zoe

+0

爲什麼不只是有不同的視圖使用不同的XML?而回收者視圖......看起來像是使用列表視圖來完成像這樣的複雜列表的很多努力 - 回收站針對這些豐富,複雜的列表 –

+0

如果您創建兩個不同的TextView背景xml文件,該怎麼辦?一個用於左側,另一個用於右側。然後如果你在你的左視圖中:textView.setBackground(getResources()。getDrawable(R.drawable.leftTextViewBackground));,並在你的右列表視圖:textView.setBackground(getResources()。getDrawable(R.drawable。 rightTextViewBackground)); ? – DadoZolic

回答

2

這裏以編程方式創建GradientDrawable形狀的示例。

GradientDrawable shape = new GradientDrawable(); 
shape.setShape(GradientDrawable.RECTANGLE); 
shape.setColor(Color.RED); 
shape.setStroke(3, Color.YELLOW); 

用於更改漸變所有角的半徑。

shape.setCornerRadius(15); 

用於更改漸變特定角的半徑。

shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 }); 

您可以使用此作爲繪製如下背景:

view.setBackgroundDrawable(shape); 
相關問題