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

时间:2013-09-11 06:14:02

标签: android user-interface view textview expandablelistview

我正在使用可扩展列表视图,我想改变我在其中使用的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>

我的子视图的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" />

我必须做的事情: -

enter image description here

到目前为止我做了什么: -

enter image description here

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

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


}

任何关于此的线索都将受到赞赏!!

3 个答案:

答案 0 :(得分:0)

使用android:shadowColor,android:shadowDx和android:shadowDy作为文本视图。 请参阅以下链接

How to add shadow to TextView on selection/focus

答案 1 :(得分:0)

将你的TextView改为bwlow
 添加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>

现在,您可以根据自己的需要编辑样式

答案 2 :(得分:0)

试试这个

  1. 如果你想改变组列表的文字样式......
    1. 您可以直接将ur headerlist.xml textview更改为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);
  2. 如果你想要儿童名单......

    1.在childlis.xml中同样使用grouplist.xml

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