简体   繁体   中英

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

i am using expandable list view and i want to change the style of textview that i am using in it.
i have to add glowing effect to the text of both expanded group view and and selected child view.

my xml code for GroupView :-

<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>

my xml code for childview :-

   <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" />

what i have to do :-

在此处输入图片说明

what i have done till now :-

在此处输入图片说明

my expandable list view Adapter :-

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;
    }


}

any leads on this will be appreciated !!

Use android:shadowColor , android:shadowDx and android:shadowDy for the text view. Refer to the below link

How to add shadow to TextView on selection/focus

Cahnge your TextView As bwlow
Add style="@style/myshadowstyle" to it

<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" />

Create styles.xml in your values folder as below

  <?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>

Now here you can edit style as it suits to you

Try this

  1. if you want to change the text style of group list ......
    1. you can directly change ur headerlist.xml textview to CustomTextview like this <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. if you want to change programmatically do this.... go to adapter class,then in your getGroupView method 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. and if you want in child list ......

    1.same as grouplist.xml do in your childlis.xml alse

    1. if you want to change programmatically do this.... go to adapter class,then in your getChildView method 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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