如何在MaterialSpinner周围放置边框Xamarin.Android?

时间:2018-02-01 08:15:38

标签: xamarin.android border spinner

我必须给我的MaterialSpinner以下边框: enter image description here

我有以下MaterialSpinner

<fr.ganfra.materialspinner.MaterialSpinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    app:ms_arrowColor="#000000"
    app:ms_arrowSize="16dp"
    app:ms_floatingLabelColor="#A9A9A9"
    app:ms_floatingLabelText="Избор на група"
    app:ms_hint="Избор на група"
    app:ms_hintColor="#000000"
    android:padding="10dp"
    android:layout_gravity="center"
    android:background="@drawable/material_spinner_border"/>

material_spinner_border.xml

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

    <!-- View background color -->
    <solid
        android:color="#000000" >
    </solid>

    <!-- View border color and width -->
    <stroke
        android:width="1dp"
        android:color="#000000" >
    </stroke>

    <!-- The radius makes the corners rounded -->
    <corners
        android:radius="2dp"   >
    </corners>

</shape>

当我运行应用程序时,边框不会出现在屏幕上

2 个答案:

答案 0 :(得分:1)

  

如何在MaterialSpinner Xamarin.Android周围放置边框?

将您的MaterialSpinner ms_arrowColor颜色设置为白色以隐藏箭头:

<fr.ganfra.materialspinner.MaterialSpinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        app:ms_floatingLabelColor="#A9A9A9"
        app:ms_floatingLabelText="Избор на група"
        app:ms_hint="Избор на група"
        app:ms_hintColor="#000000"
        app:ms_arrowColor="#ffffff"
        app:ms_arrowSize="16dp"
        android:background="@drawable/material_spinner_border"
        android:layout_margin="5dp"
        />

将箭头图像添加到项目中,例如:

this PR on GitHub

然后,像这样修改material_spinner_border

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <layer-list>
      <item>
        <shape>
          <stroke android:width="3dp" android:color="#000000" />
          <corners android:radius="4dp" />
          <padding android:bottom="0dp" android:left="3dp" android:right="3dp" android:top="0dp" />
        </shape>
      </item>
      <item >
        <bitmap android:gravity="center|right" android:src="@drawable/myarrow" />
      </item>
    </layer-list>
  </item>
</selector>

enter image description here

更新

设置Spinner适配器后设置微调器背景将解决此问题:

string[] ITEMS = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" };
var adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleSpinnerItem, ITEMS);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
var spinner = FindViewById<MaterialSpinner>(Resource.Id.spinner);
spinner.Adapter = adapter;

spinner.Background = Drawable.CreateFromXml(Resources, Resources.GetXml(Resource.Drawable.material_spinner_border));

答案 1 :(得分:0)

这是一张图片:

enter image description here

我忘了告诉你,如果重要的话我的背景是黑暗的