Android Дизайн - разные цвета вкладок по горизонтали? - PullRequest
0 голосов
/ 04 апреля 2020

Каков наилучший способ сделать что-то подобное:

image

Я узнал, что могу сделать это с помощью ImageView, но я хочу узнать, есть ли другие динамические c путь?

1 Ответ

1 голос
/ 05 апреля 2020

Вы можете использовать ListView или RecyclerView.

Вот MainActivity,

 class MainActivity : AppCompatActivity() {

   val colors = arrayOf(
    "red", "green", "blue",
    "cyan", "magenta", "yellow",   //you can also provide selected colors list
    "black", "white", "gray",
    "maroon", "maroon", "fuchsia",
    "navy", "olive", "teal"
    )

   val numList = ArrayList<Int>()

  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      for (i in 1..1000) {
          numList.add(i)
        }

      val colorAdapter = ColorAdapter(this, numList, colors)
      tvColor.adapter = colorAdapter


     }

И вы можете создать Адаптер для списка цветов

 class ColorAdapter(val context: Context, val nums: ArrayList<Int>, val cols: Array<String>): BaseAdapter() {

   override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {


    val li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    val itemView;if(convertView!=null)
    {
        convertView
    }
    else
    {
        li.inflate(R.layout.list_item_color, parent, false)
    }
    val id = nums[position]
    val colorName = cols[position % 15]
    val color = Color.parseColor(colorName)

    itemView.llColorBox.setBackgroundColor(color)
    itemView.tvColor.text = colorName
    itemView.tvId.text = id.toString()
    return itemView
   }

override fun getItem(position: Int): Any? {
    return null
   }

override fun getItemId(position: Int): Long {
    return 0
}


override fun getCount(): Int {
    return nums.size
  }

 }

activity_main. xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".MainActivity">

    <ListView
            android:id="@+id/tvColor"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</FrameLayout>

list_item_color. xml

   <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/llColorBox"
        android:padding="10dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
            android:id="@+id/tvId"
            android:textSize="20sp"
            android:gravity="right"
            android:text="10"
            android:layout_width="80dp"
            android:layout_height="wrap_content" />

    <TextView
            android:layout_marginLeft="10dp"
            android:id="@+id/tvColor"
            android:textSize="20sp"
            android:text="black"
            android:layout_width="232dp"
            android:layout_height="wrap_content"/>

</LinearLayout>

Результат

...