How to Change a size of fragment?

2019-07-21 08:24发布

问题:

Pretty new to android development, I need to know how I can change the size of my ListView in my fragment pragmatically to cover the entire screen? match_parent, fill_parent didn't work.

I have hard coded values of 500dp

Here is my fragment

<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">

    <ListView
        android:id="@android:id/list"
        android:layout_width="500dp"
        android:layout_height="500dp"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:layout_marginTop="0dp">
    </ListView>

</RelativeLayout>

and here is JAVA my fragment code:

import android.app.ListFragment;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import java.util.List;



public class MyFragment extends ListFragment {

    List<data> flowers = new datadata().getdatas();


    public MyFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


       dataArrayAdapter adapter = new dataArrayAdapter(getActivity(),
               R.layout.data_listitem,
               flowers);
       setListAdapter(adapter);


    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.my_fragment, container, false);

        return rootView;
    }

}

here is my main Activity class

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="none"
    android:fadeScrollbars="true"
    android:fitsSystemWindows="true"
    android:layout_gravity="top"
    >
</ScrollView >

and this is my main activity java code:

package mshirvan.example.com.netplex;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.view.View.OnClickListener;

import data.row1;


public class MainActivity extends Activity {

    public static float screenx = 0;
    public static float screeny = 0;
    public static int screenpixelx = 0;
    public static int screenpixely = 0;


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

        ScreenUtility utility = new ScreenUtility(this);
        screenx = utility.getWidth();
        screeny = utility.getHeight();
        screenpixelx = utility.getPixelWidth();
        screenpixely = utility.getPixelHeight();

        MyFragment frag = new MyFragment();

        getFragmentManager().beginTransaction()
                .add(R.id.myContainer, frag)
                .commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            ScreenUtility utility = new ScreenUtility(this);
            String output = "Width: " + utility.getWidth() + ", " +
                    "Height: " + utility.getHeight();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

回答1:

Add the following to your fragment RelativeLayout. This should be as follows:

android:layout_width="match_parent"
android:layout_height="500dp"

For example the map layout fragment should be as follows :

<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="500dp"
    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"
    >

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

Then add this fragment layout in the main layout.



回答2:

put these in your listview too:

android:layout_width="match_parent" android:layout_height="match_parent"

or you can change the layoutparameters to match_parent in yout fragment that contains the listview.



回答3:

you have to set the fragment container height to match parent not ListView. in your case ,ListView will fill it's parent height. ListView's parent is RelativeLayout and the RelativeLayout height is matchParent that means , RelativeLayout will also fill it's container and it's container is your fragment place holder that defined in your activity's layout file. you have to set that container's height to matchParent.



回答4:

I think the problem is that you are trying to add fragment to ScrollView. use FrameLayout with android:layout_height=match_parent for fragment's container and if you have to add multiple fragments, then define multiple FrameLayouts in your activity's layout or add them dynamically.