繁体   English   中英

如何在可扩展列表视图中更改TextView的样式?

[英]how to change the style of TextView in Expandable List View?

我正在使用可扩展列表视图,并且我想更改在其中使用的textview样式。
我必须为扩展的组视图和选定的子视图的文本添加发光效果。

我的GroupView xml代码:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >


    <TextView
        android:id="@+id/groupelement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:textSize="21dp"
        android:gravity="left"
        />

    <ImageView
        android:id="@+id/ud_image"
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:scaleType="centerInside"
        />
</LinearLayout>

我的childview的xml代码:-

   <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/submenuitem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:textSize="16dip"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp" />

我该怎么办:-

在此处输入图片说明

我到目前为止所做的事情:-

在此处输入图片说明

我的可扩展列表视图适配器:-

public class ExpandableMenuListAdapter extends BaseExpandableListAdapter {

    private LayoutInflater inflater;
    private ArrayList<ExpandableMenu> expandableMenu;
    private Context mContext;
    private Typeface tfBold;
    private Typeface tflight;
    private  Typeface tfregular;
    private TextView grpText;
    private TextView childText;
    private ImageView indicator;

    public ExpandableMenuListAdapter(ArrayList menus,Context context){
        expandableMenu=menus;
        mContext=context;
        tfBold = Typeface.createFromAsset(mContext.getAssets(), "fonts/Raleway-Bold.otf");
        tflight = Typeface.createFromAsset(mContext.getAssets(), "fonts/Raleway-Light.otf");
        tfregular = Typeface.createFromAsset(mContext.getAssets(), "fonts/Raleway-Regular.otf");
    }
    @Override
    public int getGroupCount() {
        return expandableMenu.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return expandableMenu.get(i).getSubMenu().size();
    }

    @Override
    public Object getGroup(int i) {
        return expandableMenu.get(i);
    }

    @Override
    public Object getChild(int i, int i2) {

        return expandableMenu.get(i).getSubMenu().get(i2);
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i2) {
        return i2;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean isExpanded, View view, ViewGroup viewGroup) {

        inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) {
            view = inflater.inflate(R.layout.groupview_with_child, null,false);
        }

        indicator=(ImageView)view.findViewById(R.id.ud_image);
        grpText=(TextView)view.findViewById(R.id.groupelement);
        String title = expandableMenu.get(i).getTitle();
        grpText.setText(title);
        grpText.setTypeface(tfregular);
        if ( getChildrenCount( i ) == 0 ) {
            indicator.setVisibility( View.INVISIBLE );
            view.setPadding(0, 0, 0, 20);
        } else {
            indicator.setVisibility(View.VISIBLE);
            indicator.setImageResource( isExpanded ? R.drawable.up : R.drawable.down );
            view.setPadding(0, 0, 0, 20);
            if(isExpanded) view.setPadding(0, 0, 0, 5);
        }

     /*   if(isExpanded){
            grpText.setTextAppearance(mContext,R.style.glow);
        }else{
            grpText.setTextAppearance(mContext,R.style.dim);
        }*/
        return view;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {


        super.onGroupExpanded(groupPosition);
    }

    @Override
    public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
        inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) {
            view= inflater.inflate(R.layout.submenu, null,false);
        }

        if ((12 == expandableMenu.get(i).getSubMenu().size() - 1)){
            view.setPadding(0, 0, 0, 20);
        }
       childText = (TextView) view.findViewById(R.id.submenuitem);
        //"i" is the position of the parent/group in the list and
        //"i1" is the position of the child
        //textView.setText(mParent.get(i).getArrayChildren().get(i1));
        String subTitle= expandableMenu.get(i).getSubMenu().get(i2);

        childText.setText(subTitle);
        childText.setTypeface(tfBold);

        //view.setTag(holder);

        //return the entire view
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i2) {
        return true;
    }


}

任何线索对此将不胜感激!

使用android:shadowColor,android:shadowDx和android:shadowDy进行文本视图。 请参考以下链接

如何在选择/焦点上向TextView添加阴影

将您的TextView更改为blow
向其中添加style =“ @ style / myshadowstyle”

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/submenuitem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:textSize="16dip"
    style="@style/myshadowstyle"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp" />

如下所示在您的values文件夹中创建styles.xml

  <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="myshadowstyle">   
    <item name="android:shadowColor">#ff8800</item>
    <item name="android:shadowDx">-2</item>
    <item name="android:shadowDy">-2</item>
    <item name="android:shadowRadius">1</item>
    </style>
</resources>

现在在这里您可以编辑适合您的样式

尝试这个

  1. 如果要更改组列表的文字样式...
    1. 您可以像这样<chifferobe.app.com.chifferobe.CustomTextView android:id="@+id/lblListHeader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/iv_icon" android:textColor="@color/colorWhite" android:layout_marginLeft="@dimen/_15dp" android:textSize="@dimen/_15dp" />将ur headerlist.xml文本视图直接更改为CustomTextview <chifferobe.app.com.chifferobe.CustomTextView android:id="@+id/lblListHeader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/iv_icon" android:textColor="@color/colorWhite" android:layout_marginLeft="@dimen/_15dp" android:textSize="@dimen/_15dp" />
    2. 如果要以编程方式进行更改,请执行此操作。...转到适配器类,然后在getGroupView方法TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle);
  2. 如果您想在子列表中...

    1.与grouplist.xml在您的childlis.xml中相同

    1. 如果要以编程方式进行更改,请执行此操作。...转到适配器类,然后在您的getChildView方法TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem); lblListHeader.setTypeface(null, Typeface.BOLD); txtListChild.setText(childText); TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem); lblListHeader.setTypeface(null, Typeface.BOLD); txtListChild.setText(childText);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM