2014-09-04 105 views
0

我有一个列表视图,它使用来自我的Parse.com数据库的值填充,然后显示它们。我看到字体和大小可以改变为正常列表,但他们不适合我的工作。这是我的全部代码如何更改ListView的字体大小和对齐方式

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

您必须创建自定义列表视图。很好的例子在这里:http://www.bignerdranch.com/blog/customizing-android-listview-rows-subclassing/ – Opiatefuchs 2014-09-04 13:32:23

+0

我仍然困惑如何使用它为我的情况。你有没有任何示例代码可以解释得更好一些 – Androider 2014-09-04 17:07:05

回答

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> 
相关问题