我在使用带有ListView的DialogFragment时遇到问题。当我在onCreate中使用setStyle时会出现问题。
以下是代码(为了更清楚地了解问题,我将品红色的列表文本视图着色):
import android.app.DialogFragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MenuDialog extends DialogFragment {
public static MenuDialog create() {
MenuDialog frag = new MenuDialog();
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ListView v = new ListView(getActivity());
v.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = null;
if (convertView == null) {
view = new TextView(getActivity());
view.setTextAppearance(getActivity(), android.R.style.TextAppearance_Large);
view.setPadding(15, 15, 15, 15);
view.setBackgroundColor(Color.MAGENTA);
}
else
view = (TextView) convertView;
switch (position) {
case 0: view.setText("Random"); break;
case 1: view.setText("Community favourites"); break;
case 2: view.setText("Change image"); break;
case 3: view.setText("Share"); break;
case 4: view.setText("Informations"); break;
}
return view;
}
@Override public long getItemId(int arg0) { return 0; }
@Override public Object getItem(int arg0) { return null; }
@Override public int getCount() { return 5; }
});
return v;
}
}
这是它提供的对话框的图像:
现在,如果我只是评论setStyle(STYLE_NO_TITLE,0);在onCreate方法中,一切都按预期工作:
我做错了什么? 我不能使用简单的AlertDialog,因为我需要自定义更多的列表项。
编辑: You can download here a demo Android Eclipse project that demonstrates the problem
答案 0 :(得分:2)
list.xml
<?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:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
menu_rowview.xml
<?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:orientation="horizontal"
android:id="@+id/linear"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="@android:string/unknownName"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
更改onCreateView()(在您的项目中)
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list, container);
ListView v=(ListView) view.findViewById(R.id.list);
v.setAdapter(new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = null;
if (convertView == null) {
rowView = inflater.inflate(R.layout.menu_rowview, parent, false);
}
else
rowView = convertView;
LinearLayout lay=(LinearLayout) rowView.findViewById(R.id.linear);
lay.setBackgroundColor(Color.MAGENTA);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
switch (position) {
case 0: textView.setText("Random"); break;
case 1: textView.setText("Community favourites"); break;
case 2: textView.setText("Change image"); break;
case 3: textView.setText("Share"); break;
case 4: textView.setText("Informations"); break;
}
return rowView;
}
@Override public long getItemId(int arg0) {
return arg0; }
@Override public Object getItem(int arg0) { return arg0; }
@Override public int getCount() { return 5; }
});
return view;
}
答案 1 :(得分:1)
在android:minWidth="360dp"
TextView
答案 2 :(得分:1)
在onCreate方法中使用此代码
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
答案 3 :(得分:0)
这个问题让我整天疯狂,这绝对是一个系统中的错误。我最终要做的就是我离开STYLE_NO_TITLE,因为我真的不想处理标题,我的其他对话格式主题是:
fragment.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog);
这当然会在某些手机上带来问题,这不仅仅是背景问题,也是一个可触摸的领域。现在令人讨厌的hacky修复在我的项目xml布局中:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="48dp"
android:width="600dp"
android:textSize="19sp"
android:textColor="@color/blue"
android:paddingLeft="16dp"
android:gravity="center_vertical"
/>
硬编码宽度到大值是一个技巧,但我不太确定它如何影响lo-res设备的性能。在Kindle Fire,Nexus 7和2.2的仿真器上进行了测试 希望这会帮助某人