无法将ListView / DialogFragment设置为全屏

时间:2015-09-22 13:51:42

标签: android listview android-fragments



<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:padding="10dp"
        android:src="@drawable/arteta"/>

     <TextView
         android:id="@+id/nameTv"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_marginLeft="27dp"
         android:layout_marginTop="10dp"
         android:layout_toRightOf="@id/imageView1"
         android:text="name"/>



</RelativeLayout>
&#13;
&#13;
&#13;

&#13;
&#13;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:padding="10dp"
        android:id="@+id/imageView1"
        android:src="@drawable/arteta"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="27dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="Name"
        android:id="@+id/nameTv" />



</RelativeLayout>
&#13;
&#13;
&#13;

&#13;
&#13;
RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show"
    android:id="@+id/button1"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="setContentView" />
</RelativeLayout>
&#13;
&#13;
&#13;

我使用DialogFragment创建了一个ListView,我试图以全屏显示它 但是,当我在设备上运行它时,ListView的边缘周围总是有一个边框,就像一个框内的框一样,似乎无法将其设置为填满屏幕!

我尝试过很多路线让这个ListView覆盖整个屏幕无济于事 我非常希望并感谢你们的帮助。

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ></ListView>


</RelativeLayout>

MainActivity

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.FragmentManager;


public class MainActivity extends FragmentActivity {

    Button showBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        final PlayersFragment p = new PlayersFragment();
        showBtn = (Button) findViewById(R.id.button1);
        showBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                p.show(fm, "Players Fragment");
            }


    });
}
}

PlayersFragment

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class PlayersFragment extends android.support.v4.app.DialogFragment {

    ListView lv;
    String[] players = {"micheal arteta", "deago costa", "andy reid", "scum degea", "scum rooney", "john terry"};

    int[] images = {R.drawable.arteta, R.drawable.costa, R.drawable.reid, R.drawable.degea,
            R.drawable.rooney, R.drawable.terry};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialog, container, false);

        //initialize listview
        lv = (ListView) rootView.findViewById(R.id.listView1);

        //set dialog title
        getDialog().setTitle("Soccer SuperStars");

        //create adapter obj and set list view to it
        Adapter adapter = new Adapter(getActivity(), players);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
                Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
            }
        });


        return rootView;


    }

}

适配器

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Adapter extends ArrayAdapter<String>{

    Context c;
    String[] players;
    int[] images;
    LayoutInflater inflater;

    public Adapter(Context context, String[] players) {
        super(context, R.layout.rowmodel,players);

    this.c=context;
        this.players = players;
        this.images = images;
    }

    @Override
    public View getView(int position,View convertView, ViewGroup parent){
        if(convertView==null)
        {
       inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.rowmodel,null);
        }
        TextView nameTv = (TextView) convertView.findViewById(R.id.nameTv);
        ImageView img = (ImageView) convertView.findViewById(R.id.imageView1);

        nameTv.setText(players[position]);
        img.setImageResource(images[position]);
        return convertView;

    }

}

2 个答案:

答案 0 :(得分:1)

我认为你应该在main_activity布局中使用FrameLayout。像这样:

<RelativeLayout
    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"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show"
            android:id="@+id/button1"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:onClick="setContentView" />

    </FrameLayout>

</RelativeLayout>

然后,在您的MainActivity中,试试这个:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends FragmentActivity {

Button showBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    final PlayersFragment p = new PlayersFragment();
    showBtn = (Button) findViewById(R.id.button1);
    showBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            showBtn.setVisibility(View.GONE);

            transaction.replace(R.id.fragment_container, p);
            transaction.addToBackStack(null);
            transaction.commit();
        }


    });
}

@Override
public void onBackPressed() {
    if (showBtn != null) {
        showBtn.setVisibility(View.VISIBLE);
    }
    super.onBackPressed();
}
}

您的适配器:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Adapter extends ArrayAdapter<String>{

    Context c;
    String[] players;
    int[] images;
    LayoutInflater inflater;

    public Adapter(Context context, String[] players, int[] images) {
        super(context, R.layout.simplerow,players);

        this.c=context;
        this.players = players;
        this.images = images;
    }

    @Override
    public View getView(int position,View convertView, ViewGroup parent){
        if(convertView==null)
        {
            inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.simplerow,null);
        }
        TextView nameTv = (TextView) convertView.findViewById(R.id.nameTv);
        ImageView img = (ImageView) convertView.findViewById(R.id.imageView1);

        nameTv.setText(players[position]);
        img.setImageResource(images[position]);
        return convertView;

    }

}

您的对话框布局:

<?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="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Soccer SuperStars"
        android:layout_gravity="center"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@android:color/black"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ></ListView>


</LinearLayout>

你的PlayerFragment:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class PlayersFragment extends android.support.v4.app.DialogFragment {

    ListView lv;
    String[] players = {"micheal arteta", "deago costa", "andy reid", "scum degea", "scum rooney", "john terry"};

    int[] images = {R.drawable.arteta, R.drawable.costa, R.drawable.reid, R.drawable.degea,
            R.drawable.rooney, R.drawable.terry};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialog, container, false);

        //initialize listview
        lv = (ListView) rootView.findViewById(R.id.listView1);


        //create adapter obj and set list view to it
        Adapter adapter = new Adapter(getActivity(), players, images);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
                Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
            }
        });


        return rootView;


    }

}

我从main_activity布局中删除了一些来自(填充)并包含一些代码显示“对话框”(现在是一个片段)全屏。

答案 1 :(得分:0)

1)第一种方法

 public class PlayersFragment extends DialogFragment {
 @Override
 public void onCreate(@Nullable Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

    setStyle(DialogFragment.STYLE_NO_TITLE,    android.R.style.Theme_NoTitleBar_Fullscreen);
}
 }

2)第二种方法

  public class PlayersFragment extends DialogFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myCustomStyle);

     }
     }

in values add style

 <?xml version="1.0" encoding="utf-8"?>
 <resources>

 <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">     
 </style>

 <!-- Application theme. -->
 <style name="AppTheme" parent="AppBaseTheme">       
</style>
<style name="GalleryDialogStyle" parent="AppTheme">       
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>
</resources>