我有一個機器人DialogFragment一個顯示的ListView,含有一個標題和圖像的每一行。一切都顯示正常,直到我從DialogFragment刪除標題與getDialog()。getWindow()。requestFeature(Window.FEATURE_NO_TITLE);的Android DialogFragment No_Title功能會影響佈局
一旦我刪除標題,圖像停止顯示在ListView中,所有顯示的都是標題。 FEATURE_NO_TITLE如何可能影響DialogFragment中ListView的顯示?從字面上看,沒有其他代碼行改變 - 只有那一行,圖像停止在ListView中顯示。
對話片段的OnCreate
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pick_photo_dialog_fragment, container);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Button closeButton = (Button) view.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
listview = (ListView) view.findViewById(R.id.photosListView);
listadapter = new PhotoListAdapter(getActivity());
listview.setAdapter(listadapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//do stuff
}
});
return view;
}
XML的片段包含比的LinearLayout內ListView和按鈕而已...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/dark_gray">
<ListView android:id="@+id/photosListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:clickable="false"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
/>
<Button android:id="@+id/closeButton"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="Close"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="sans"
android:background="@color/transparent"/>
</LinearLayout>
ListAdapter相關類
int[] mResources = {R.drawable.basketball, R.drawable.bodyweight, R.drawable.boulder, R.drawable.boxing, R.drawable.cardio,
R.drawable.crossfit, R.drawable.olympic, R.drawable.racquet, R.drawable.run, R.drawable.spin, R.drawable.squash,
R.drawable.swim, R.drawable.volleyball, R.drawable.weight, R.drawable.yoga};
String[] pictureNames = {"basketball", "bodyweight", "boulder", "boxing", "cardio", "crossfit", "olympic", "racquet", "run",
"spin", "squash", "swim", "volleyball", "weight", "yoga"};
public PhotoListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mContext = context;
}
public int getCount() {
return mResources.length;
}
public Object getItem(int position) {
return pictureNames[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.photo_list_row_view, null);
holder = new ViewHolder();
holder.activityNameTV = (TextView) convertView.findViewById(R.id.activityName);
holder.activityImageIV = (ImageView) convertView.findViewById(R.id.activityImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
task = new CreateClassSetImageTask(position, holder.activityNameTV, holder.activityImageIV);
task.execute();
return convertView;
}
static class ViewHolder {
TextView activityNameTV;
ImageView activityImageIV;
}
XML的ListView的行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/activityName"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp"
android:textStyle="normal"
android:textColor="#FFFFFF"
android:typeface="sans"
android:layout_gravity="left|center_vertical"
/>
<ImageView android:id="@+id/activityImage"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:scaleType="centerInside"/>
</LinearLayout>
異步任務載入圖像
public class CreateClassSetImageTask extends AsyncTask<Void, Void, Bitmap> {
WeakReference<ImageView> imageViewReference;
WeakReference<TextView> textViewReference;
int pictureNum;
public CreateClassSetImageTask(int pictureNumIn, TextView activityNameTVIn, ImageView activityIVIn) {
pictureNum = pictureNumIn;
imageViewReference = new WeakReference<ImageView>(activityIVIn);
textViewReference = new WeakReference<TextView>(activityNameTVIn);
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Bitmap resultBitmap) {
if (imageViewReference != null && resultBitmap != null && textViewReference != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(resultBitmap);
}
final TextView textView = textViewReference.get();
if (textView != null){
textView.setText(pictureNames[pictureNum]);
}
}
}
@Override
protected Bitmap doInBackground(Void... params) {
return decodeSampledBitmapFromResource(mContext.getResources(), mResources[pictureNum], 100, 100);
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) > reqHeight
&& (halfWidth/inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
通常我把requestFeature指令中的onResume。在onCreateView它似乎並沒有工作(即使我從來沒有遇到過你的問題)。如果你嘗試這樣做會發生什麼? – thetonrifles
@thetonrifles感謝您的想法 - 我嘗試了它,但它引發了一個運行時異常「requestFeature()必須在添加內容之前調用」 - 因爲onCreateView(或者即使我切換到使用onCreate)總是在onResume之前調用,我不知道我會如何解決這個問題 - 在訪問onResume之前必須添加內容,這意味着我無法在onResume中調用requestFeature()。如何讓requestFeature()在onResume中工作?無論哪種方式,我不知道這是這裏的問題 - 當我稱之爲,它的作品 - 標題消失。它只是破壞其餘的片段以及... –
對不起我的錯誤。我混淆了背景和寬度/高度的定義。嘗試在onCreateDialog方法中添加requestFeature。 – thetonrifles