【问题标题】:DropDownTextView Android change colorDropDown TextView Android 改变颜色
【发布时间】:2021-05-14 11:03:16
【问题描述】:

我在 0.1.1 版本中使用 https://github.com/hakobast/DropdownTextView 0.3.1 版不适合我。 我想知道当我单击 DropdownTextView 时是否有机会更改框架颜色。 第二件事是如何将 DropdownTextView 标题移到中心? 如您所见,我在中心模式下拥有所有重力并且没有任何反应。

我的代码:

MainActivity.kt

package com.example.myapplication

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.*
import android.widget.*
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import hakobastvatsatryan.DropdownTextView


class MainActivity : AppCompatActivity()
{
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var zwrotkaString = """{

}"""
        var stat:Statistic = Gson().fromJson(zwrotkaString, Statistic::class.java)

        val d = findViewById<ListView>(R.id.first_dropdown_text_view_list)

        d.adapter = MyCustomAdapter(this@MainActivity, stat);
    }

    private class MyCustomAdapter(context: Context, stat: Statistic): BaseAdapter()
    {
        val statS:Statistic

        private val mContext: Context
        init
        {
            mContext = context
        }

        init
        {
            statS = stat
        }

        override fun getCount(): Int {
            return statS.statisticsForGroups.size;
        }

        override fun getItem(position: Int): Any
        {
            return statS.statisticsForGroups[position];
        }

        override fun getItemId(position: Int): Long
        {
            return position.toLong();
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View
        {
            val layoutInflater = LayoutInflater.from(mContext);
            val rowMain = layoutInflater.inflate(R.layout.rowlistviewstatistic, parent, false)
            val statDropDownText = rowMain.findViewById<DropdownTextView>(R.id.dropdowntextrow)
            
                statDropDownText.setTitleText("Title")
                statDropDownText.setContentText("Content")

                return rowMain

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start"
    android:layout_gravity="center"
    android:gravity="center"
    >


    <ListView
        android:id="@+id/first_dropdown_text_view_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@android:color/transparent"
        android:dividerHeight="20sp"
        android:scrollbarStyle="insideInset"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewMain"
        app:layout_constraintVertical_bias="0.403"
        tools:ignore="MissingConstraints"
        android:layout_gravity="center"

        />


</LinearLayout>

rowlistviewstatistic.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="qweqdasdasdasdadaq aa fadfeaeef"
        />

    <hakobastvatsatryan.DropdownTextView
        android:id="@+id/dropdowntextrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:content_text_color="@color/purple_500"
        app:arrow_drawable="@drawable/ic_arrow"
        app:title_text_color="@color/colorRed"
        android:layout_gravity="center"
        android:gravity="center"
        app:title_text_size="30sp"
        app:bg_drawable_expanded="@drawable/answer_drawable"
        android:clickable="true"
        android:focusable="true"
        />

</LinearLayout>

answer_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="@color/answer_border" />
    <corners android:radius="5dp" />
</shape>

【问题讨论】:

    标签: android kotlin drop-down-menu


    【解决方案1】:

    我想知道当我点击 DropdownTextView 时是否有机会改变框架颜色

    OnClickListener 添加到您的TextView 中,一旦用户单击它就会更改框架颜色

    statDropDownText.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
         statDropDownText.setRegularBackgroundDrawableRes(R.drawable.answer_drawable)
    
        }
    });
    

    第二件事是如何将 DropdownTextView 标题移到中心?

    查看源代码后,我发现了这个methodgetTitleTextView()。 调用它会返回TextView,并将重心设置为中心。

    TextView ta = (TextView) getTitleTextView();
    LayoutParams lp = new LayoutParams();
    lp.gravity = Gravity.CENTER_HORIZONTAL;
    ta.setLayoutParams(lp);
    

    【讨论】:

    • 感谢您的回答,但我在 0.1.1 版本中没有这些选项
    • @BinaryMan “但我在 0.1.1 版本中没有这些选项”你的意思是他们两个
    • statDropDownText.setBackgroundResource() 这在 kotlin 中都可用 0.1.1 版本 0.3.1 对我来说不能正常工作
    • getTitleTextView() 是否适合您。为什么不调用它,并在 textview 上使用 stackoverflow.com/a/6932112/9846650 而不是 statDropDownText.setBackgroundResource()
    • 我查了告诉你
    【解决方案2】:

    我修复了 0.3.1 崩溃并发布了 0.3.3,请尝试让我知道它是否解决了您的问题。 我不是我分叉它并修复了崩溃的库的所有者,并且必须在 jitpack 上发布它,而不是像它已弃用的 bintray(jcentre)。

    有关集成库的详细信息,请参见

    https://stackoverflow.com/a/67520594/4828650

    【讨论】:

      猜你喜欢
      • 2011-05-19
      • 2016-08-05
      • 1970-01-01
      • 2023-04-04
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      相关资源
      最近更新 更多