Android设计-不同颜色标签水平?
这样做的最佳方式是什么:Android设计-不同颜色标签水平?,android,xml,Android,Xml,这样做的最佳方式是什么: 我发现我可以用ImageView来做,但我想知道还有其他动态方式吗?您可以使用ListView或RecyclerView 这里是主要活动 class MainActivity : AppCompatActivity() { val colors = arrayOf( "red", "green", "blue", "cyan", "magenta", "yellow", //you can also provide selected co
我发现我可以用ImageView来做,但我想知道还有其他动态方式吗?您可以使用ListView或RecyclerView 这里是主要活动
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 MainActivity:AppCompatActivity(){
val colors=arrayOf(
“红”、“绿”、“蓝”,
“青色”、“洋红”、“黄色”//您还可以提供所选颜色列表
“黑色”、“白色”、“灰色”,
“褐红色”、“褐红色”、“紫红色”,
“海军蓝”、“橄榄色”、“青色”
)
val numList=ArrayList()
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
对于(1..1000中的i){
numList.add(i)
}
val colorAdapter=colorAdapter(此,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
}
}
类ColorAdapter(val-context:context,val-nums:ArrayList,val-cols:Array):BaseAdapter(){
覆盖视图(位置:Int,转换视图:View?,父视图:ViewGroup?):视图{
val li=context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE)作为LayoutInflater
val itemView;如果(convertView!=null)
{
convertView
}
其他的
{
li.充气(R.layout.list\u item\u color,parent,false)
}
val id=nums[位置]
val colorName=cols[位置%15]
val color=color.parseColor(colorName)
itemView.llColorBox.setBackgroundColor(颜色)
itemView.tvColor.text=colorName
itemView.tvId.text=id.toString()
返回项目视图
}
覆盖趣味getItem(位置:Int):有吗{
返回空
}
覆盖getItemId(位置:Int):长{
返回0
}
重写fun getCount():Int{
返回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>
它不是
选项卡
它是列表视图
所以使用RecyclerView
。我能得到这个的xml文件吗?我已经添加了xml文件作为回答。