gpt4 book ai didi

android - 如何为 Spinner DropDown 设置圆角?

转载 作者:行者123 更新时间:2023-12-04 23:44:19 36 4
gpt4 key购买 nike

我需要自定义微调器 DropDown。它应该是圆角。
现在看起来像这样:
Closed
Open
这个solution不起作用。
布局:

    <androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/messageSubject"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="@style/Text.Default.Normal"
android:background="@color/transparent"
android:minHeight="@dimen/grid_6_25"
android:layout_marginTop="@dimen/grid_1_75"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/supportQuestion" />
分段:
class SupportFragment : Fragment(R.layout.fragment_support) {

private val viewBinding by viewBinding<FragmentSupportBinding>()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initView()
}

private fun initView() {
val adapter = ArrayAdapter.createFromResource(
requireContext(),
R.array.message_subject,
R.layout.spinner_selected
).also { adapter ->
adapter.setDropDownViewResource(R.layout.spinner_dropdown)
}

viewBinding.messageSubject.adapter = NothingSelectedSpinnerAdapter(requireContext(), adapter, R.layout.spinner_hint)
}

companion object {
fun newInstance() = SupportFragment()
}
}

最佳答案

  • 创建圆形背景 spinnerbg.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#3B3B3B" />
    <corners android:radius="@dimen/_4sdp" />
    </shape>
  • 将其添加到样式。
    <style name="SpinnerTheme" parent="android:Widget.Material.Spinner.Underlined">
    <item name="android:background">@drawable/spinnerbg
    </item>
    <item name="android:popupBackground">@drawable/spinnerbg
    </item>

    <item name="android:textAlignment">textStart</item>
    </style>
  • 使用样式
    <androidx.appcompat.widget.AppCompatSpinner
    android:id="@+id/spinner_season"
    style="@style/SpinnerTheme"
    android:layout_width="@dimen/_100sdp"
    android:layout_height="@dimen/_30sdp"
    android:layout_alignParentEnd="true"
    android:layout_gravity="end"
    android:layout_marginEnd="@dimen/_10sdp"
    android:spinnerMode="dropdown" />

  • 如果需要,在 java 文件中添加自定义布局。
        ArrayAdapter aa = new ArrayAdapter(context, R.layout.item_spinner, R.id.textview, season1);
    aa.setDropDownViewResource(R.layout.item_spinner_dropdown);

    关于android - 如何为 Spinner DropDown 设置圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68982218/

    36 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com