我正在尝试使用两个单独的文本视图创建自定义可展开列表视图。一个textview需要左对齐,另一个右对齐。任何人都可以帮助我。
这是我的布局文件。
<?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/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:text="@string/click"
android:textColor="@color/Red" />
</LinearLayout>
这是我的适配器类
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.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
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.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
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 class HomeFragment extends Fragment {
public HomeFragment(){}
TextView Username;
TextView Acct, Paid, Email;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
List<String> horse;
List<String> owner ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
expListView = (ExpandableListView) rootView.findViewById(R.id.list);
// preparing list data
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Rider and Trainer Info");
listDataHeader.add("Horse and Owner Info");
// Adding child data
horse = new ArrayList<String>();
horse.add("horse");
owner = new ArrayList<String>();
listDataChild.put(listDataHeader.get(0), horse); // Header, Child data
listDataChild.put(listDataHeader.get(1), owner);
Context context = getActivity().getApplicationContext();
listAdapter = new ExpandableListAdapter(context, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
final GlobalClass globalVariable = (GlobalClass) getActivity().getApplicationContext();
String Name = globalVariable.getName();
String acct = globalVariable.getUsersid();
String paid = globalVariable.getPaid();
String email = globalVariable.getEmail();
Username = (TextView)rootView.findViewById(R.id.textView2);
Acct = (TextView)rootView.findViewById(R.id.textView4);
Paid = (TextView)rootView.findViewById(R.id.textView3);
Email = (TextView)rootView.findViewById(R.id.textView5);
Username.setText("Username: "+ Name);
Acct.setText("Acct # "+ acct);
Paid.setText("Expired On: "+ paid);
Email.setText("Email: " + email);
return rootView;
}
}
我希望每行的输出布局如下所示。
示例文本日期
答案 0 :(得分:1)
从布局中找到另一个视图
@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.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
TextView lblListDate = (TextView) convertView
.findViewById(R.id.lblListDate);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(date);
return convertView;
}
答案 1 :(得分:0)
试试这个:
<?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:padding="10dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/lblListItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="lblListItem"
android:textSize="17sp" />
<TextView
android:id="@+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Test"
android:textSize="17sp" />
</LinearLayout>
答案 2 :(得分:0)
如果您改为使用RelativeLayout。您可以使用layout_align(左/右)xml属性将TextViews左/右对齐。
喜欢这个
android:layout_alignRight = "true"