如何更改ListView的字体大小和对齐方式

时间:2014-09-04 13:28:00

标签: android android-listview parse-platform

我有一个列表视图,它会从我的Parse.com数据库中填充值,然后显示它们。我看到如何更改普通列表的字体和大小,但它们不适用于我的。这是我的整个代码

public class OfferPage extends Activity {
    String obj;
    ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offer_page);

        Intent i = getIntent();
        obj = i.getStringExtra("RestName");
        populateList(obj, "Restraunt");
    }

    private void populateList(final String Value, final String Key) {
        ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {

            @Override
            @SuppressWarnings({ "unchecked", "rawtypes" })
            public ParseQuery create() {
                ParseQuery query = new ParseQuery("Offers");
                query.whereEqualTo(Key, Value);
                return query;
            }
        };
        ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(
                this, factory);
        adapter.setTextKey("Offer");
        adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {

            @Override
            public void onLoading() {
                mProgressDialog = new ProgressDialog(OfferPage.this);
                mProgressDialog.setTitle("Searching for Offers");
                mProgressDialog.setMessage("Loading...");
                mProgressDialog.setIndeterminate(false);
                mProgressDialog.show();
            }

            @Override
            public void onLoaded(List<ParseObject> objects, Exception e) {
                mProgressDialog.dismiss();
            }
        });

        final ListView listView = (ListView) findViewById(R.id.offerList);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.offer_page, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我似乎无法弄清楚要做什么,从其他问题中读出一些建议,但他们不工作

1 个答案:

答案 0 :(得分:0)

也许这可能有所帮助,它是一个可扩展的列表

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.xx_listview_child, null);
    }

    TextView txtListChild = (TextView) convertView.findViewById(R.id.child);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.xx_listview_item, null);
    }

    TextView lblListHeader = (TextView) convertView.findViewById(R.id.myitem);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public boolean shouldExpand(boolean expand) {
    return expand;
}

}

xx_listview_child.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="55dip"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/child"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:textColor="#FFFFFF" />

</LinearLayout>

xx_listview_intem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp" >

    <TextView
        android:id="@+id/myitem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:textColor="#33b5e5" />

</LinearLayout>
相关问题