我有listview和两个textview“title”和“subtitle”作为listitem的一部分。现在在一些列表项目中我只显示“标题”而不是“副标题”,在这种情况下如何在中心对齐“标题”?下面是截图和xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="8dp">
<LinearLayout
android:id="@+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dip">
<TextView
android:id="@+id/list_item_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textColor="#666666"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textSize="18sp"
/>
<TextView
android:id="@+id/list_item_entry_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666666"
android:gravity="center"
android:layout_alignLeft="@id/list_item_entry_title"
android:layout_below="@id/list_item_entry_title"
android:singleLine="true"
android:textSize="13sp"
/>
</LinearLayout>
<ImageView
android:id="@+id/lefticons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="15dip"
android:src="@drawable/icon" />
<ImageView
android:id="@+id/righticons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dip"
android:src="@drawable/icon" />
</RelativeLayout>
以编程方式我尝试设置setMargins,但它搞砸了。还有其他方法吗?
if (ei.getTitle().contentEquals("TV Results")) {
rightimageicon.setImageResource(R.drawable.arrow);
leftimageicon.setVisibility(View.GONE);
subtitle.setVisibility(View.GONE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)title.getLayoutParams();
params.setMargins(5, 5, 5, 5); //substitute parameters for left, top, right, bottom
title.setLayoutParams(params);
}
完整的getView代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
EntryItem ei = (EntryItem)i;
v = vi.inflate(R.layout.list_item_entry_settings, null);
final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);
final ImageView rightimageicon = (ImageView)v.findViewById(R.id.righticons);
final ImageView leftimageicon = (ImageView)v.findViewById(R.id.lefticons);
if (title != null)
title.setText(ei.getTitle());
if(subtitle != null)
subtitle.setText(ei.getSubtitle());
if (ei.getTitle().contentEquals("Weekly Activity Status")) {
rightimageicon.setImageResource(R.drawable.arrow);
leftimageicon.setVisibility(View.GONE);
subtitle.setVisibility(View.GONE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)title.getLayoutParams();
params.setMargins(7, 7, 7, 7); //substitute parameters for left, top, right, bottom
title.setLayoutParams(params);
//rightimageicon.setLayoutParams(params);
}
else if (ei.getTitle().contentEquals("TV Results")) {
rightimageicon.setImageResource(R.drawable.arrow);
leftimageicon.setVisibility(View.GONE);
subtitle.setVisibility(View.GONE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)title.getLayoutParams();
params.setMargins(5, 5, 5, 5); //substitute parameters for left, top, right, bottom
title.setLayoutParams(params);
}
else if (ei.getTitle().contentEquals("MI App")) {
if(prefs.getRoamDataAllowed(context)){
rightimageicon.setImageResource(R.drawable.switch_on);
button01pos = 1;
}else{
rightimageicon.setImageResource(R.drawable.switch_off);
button01pos = 0;
}
rightimageicon.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (button01pos == 0) {
rightimageicon.setImageResource(R.drawable.switch_on);
button01pos = 1;
} else if (button01pos == 1) {
rightimageicon.setImageResource(R.drawable.switch_off);
button01pos = 0;
}
}
});
leftimageicon.setVisibility(View.GONE);
subtitle.setVisibility(View.GONE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)title.getLayoutParams();
params.setMargins(5, 5, 5, 5); //substitute parameters for left, top, right, bottom
title.setLayoutParams(params);
}
else if (ei.getTitle().contentEquals("MI VPN")) {
if (vpnenableboolean && currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH && !prefs.getTunInterfaceNotAvilableStatus(context)) {
rightimageicon.setImageResource(R.drawable.switch_on);
leftimageicon.setVisibility(View.GONE);
subtitle.setVisibility(View.GONE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)title.getLayoutParams();
params.setMargins(5, 5, 5, 5); //substitute parameters for left, top, right, bottom
title.setLayoutParams(params);
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH && !prefs.getTunInterfaceNotAvilableStatus(context)) {
if (SettingsActivity.profile_name != null) {
try {
current_state = defaultprefs.getString("currentstate",
"DISCONNECTED");
if (current_state.equalsIgnoreCase("CONNECTED")
&& OpenVPNService.current_state
.equalsIgnoreCase("CONNECTED")) {
rightimageicon.setImageResource(R.drawable.switch_on);
} else {
rightimageicon.setImageResource(R.drawable.switch_off);
}
} catch (Exception e) {
rightimageicon.setImageResource(R.drawable.switch_off);
}
} else {
rightimageicon.setImageResource(R.drawable.switch_off);
}
}
}
}
else if (ei.getTitle().contentEquals("Facebook")) {
leftimageicon.setImageResource(R.drawable.facebook);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)title.getLayoutParams();
params.setMargins(50, 0, 0, 0); //substitute parameters for left, top, right, bottom
title.setLayoutParams(params);
if(!prefs.getFbTokenStatus(context)){
subtitle.setVisibility(View.VISIBLE);
rightimageicon.setImageResource(R.drawable.wrong);
}else{
subtitle.setVisibility(View.INVISIBLE);
rightimageicon.setImageResource(R.drawable.ok);
}
}
else if (ei.getTitle().contentEquals("Twitter")) {
leftimageicon.setImageResource(R.drawable.twitter);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)title.getLayoutParams();
params.setMargins(50, 0, 0, 0); //substitute parameters for left, top, right, bottom
title.setLayoutParams(params);
if(!prefs.getTwTokenStatus(context)){
subtitle.setVisibility(View.VISIBLE);
rightimageicon.setImageResource(R.drawable.wrong);
}else{
subtitle.setVisibility(View.INVISIBLE);
rightimageicon.setImageResource(R.drawable.ok);
}
}
else {
rightimageicon.setVisibility(View.GONE);
leftimageicon.setVisibility(View.GONE);
}
}
}
return v;
答案 0 :(得分:1)
试试这个..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:padding="8dp">
<ImageView
android:id="@+id/lefticons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="15dip"
android:src="@drawable/icon" />
<LinearLayout
android:id="@+id/Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="15dip">
<TextView
android:id="@+id/list_item_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center"
android:textColor="#666666"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textSize="18sp"
/>
<TextView
android:id="@+id/list_item_entry_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666666"
android:gravity="center"
android:layout_alignLeft="@id/list_item_entry_title"
android:layout_below="@id/list_item_entry_title"
android:singleLine="true"
android:textSize="13sp"
/>
</LinearLayout>
<ImageView
android:id="@+id/righticons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dip"
android:src="@drawable/icon" />
</LinearLayout>
答案 1 :(得分:0)
将内部LinearLayout
更改为RelativeLayout
答案 2 :(得分:0)
<RelativeLayout
android:id="@+id/Text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dip"
android:orientation="vertical" >
<TextView
android:id="@+id/list_item_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textColor="#666666"
android:textSize="18sp" />
<TextView
android:id="@+id/list_item_entry_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/list_item_entry_title"
android:layout_below="@id/list_item_entry_title"
android:gravity="center"
android:singleLine="true"
android:textColor="#666666"
android:textSize="13sp" />
</RelativeLayout>
答案 3 :(得分:0)
尝试使用 RelativeLayout ,当字幕消失时,title将填充RelativeLayou。
答案 4 :(得分:0)
没有找到我通过这种方式解决的任何解决方案:
android:visibility="gone"
尝试删除文字视图
<强> {{1}} 强>