【问题标题】:Android Xamarin: Add programmatically Radio Buttons with ColorAndroid Xamarin:以编程方式添加带颜色的单选按钮
【发布时间】:2017-03-22 07:49:16
【问题描述】:

我正在尝试构建这样的东西:

我的应用获得了一个问答数组。对于每个答案,我需要以编程方式创建一个 RadioButton(例如图片中的 4 个单选按钮)。

所以我的第一个问题是,您会使用 RadioButtons 还是您认为另一种解决方法会做得更好?

我的第二个问题:是否可以更改每个单选按钮的颜色,或者我如何给每个单选按钮一个小视图,我可以在其中设置背景颜色(有按钮,然后是一个小正方形单选按钮的颜色和文本)

也许你可以帮助我:)

【问题讨论】:

    标签: android xamarin colors radio-button programmatically


    【解决方案1】:

    所以我的第一个问题是,您会使用 RadioButtons 还是您认为另一种解决方法会做得更好?

    是的,RadioButton 适合这份工作。

    是否可以更改每个单选按钮的颜色

    是的,这是可能的。只需为android:buttonRadioButton 设置可绘制资源。例如:

    <RadioGroup android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:orientation="vertical"
                android:layout_marginLeft="5dp">
      <RadioButton android:id="@+id/ware1"
                   android:layout_height="wrap_content"
                   android:layout_width="wrap_content"
                   android:text="@string/Ware1"
                   android:button="@drawable/gradient"
                   android:paddingLeft="8dp" />
      <RadioButton android:id="@+id/ware2"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:text="@string/Ware2" />
      <RadioButton android:id="@+id/ware3"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:text="@string/Ware3" />
      <RadioButton android:id="@+id/ware4"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:text="@string/Ware4" />
    </RadioGroup>
    

    我只为第一个RadioButton创建了一个选择器,这个按钮的背景颜色是渐变的,你可以以此为例创建你的:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_checked="false">
        <layer-list>
          <item android:height="25dp" android:width="25dp">
            <shape android:shape="oval">
              <gradient
                  android:startColor="#E0FFCD"
                  android:endColor="#42FAA1"
                  android:angle="45" />
              <stroke android:width="2dp"
                      android:color="#000000" />
            </shape>
          </item>
        </layer-list>
      </item>
      <item android:state_checked="true">
        <layer-list>
          <item android:height="25dp" android:width="25dp">
            <shape android:shape="oval">
              <gradient
                  android:startColor="#E0FFCD"
                  android:endColor="#42FAA1"
                  android:angle="45" />
              <stroke android:width="2dp"
                      android:color="#2d1717" />
            </shape>
          </item>
          <item android:height="10dp" android:width="10dp"
                android:gravity="center">
            <shape android:shape="rectangle">
              <solid android:color="#000000" />
            </shape>
          </item>
        </layer-list>
      </item>
    </selector>
    

    如果你觉得为每个按钮创建形状太麻烦,你可以使用图像资源来替换它们,无论如何你需要为每个RadioButton创建选择器,而选择器是为checked和@ 987654329@RadioButton的状态。

    【讨论】:

    • 感谢您的回答 :) 这对我帮助很大。但最后,我正在使用 radioButtons[i].SetCompoundDrawablesRelativeWithIntrinsicBounds(square, null, null, null); 并使用一个小白色方块并将 Tint 设置为颜色,因为我需要动态更改它。
    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2020-05-22
    相关资源
    最近更新 更多