如何更改片段的大小?

问题描述 投票:2回答:4

对Android开发很新,我需要知道如何在我的片段中实际更改ListView的大小以覆盖整个屏幕? match_parent,fill_parent不起作用。

我有500dp的硬编码值

这是我的片段

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

这里是JAVA我的片段代码:

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;
    }

}

这是我的主要Activity类

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

这是我的主要活动java代码:

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);
    }
}
android android-layout android-intent android-activity android-fragments
4个回答
1
投票

将以下内容添加到片段RelativeLayout中。这应该如下:

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

例如,地图布局片段应如下所示:

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

然后在主布局中添加此片段布局。


0
投票

把它们放在你的列表视图中:

android:layout_width =“match_parent”android:layout_height =“match_parent”

或者您可以在包含listview的片段中将布局参数更改为match_parent。


0
投票

你必须设置片段容器高度以匹配父类而不是ListView。在您的情况下,ListView将填充它的父高度。 ListView的父级是RelativeLayout,RelativeLayout的高度是matchParent,这意味着,RelativeLayout也将填充它的容器,它的容器是你的活动的布局文件中定义的片段占位符。你必须将容器的高度设置为matchParent。


0
投票

我认为问题是你正在尝试将片段添加到ScrollView。将FrameLayoutandroid:layout_height=match_parent一起用于片段容器,如果必须添加多个片段,则在活动的布局中定义多个FrameLayouts或动态添加它们。

© www.soinside.com 2019 - 2024. All rights reserved.