【问题标题】:How to Change a size of fragment?如何更改片段的大小?
【发布时间】:2014-11-14 19:43:44
【问题描述】:

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

}

这是我的主要活动类

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

【问题讨论】:

  • 请发布您的活动代码和布局
  • schemas.android.com/apk/res/android" xmlns:tools="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中添加片段吗?请编辑您的问题并在其中添加您的活动代码及其布局。
  • 是的,我马上添加,mer30 :)
  • 你为什么使用 ScrollView 作为片段容器?

标签: android android-layout android-intent android-activity android-fragments


【解决方案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>

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

【讨论】:

    【解决方案2】:

    将这些也放入您的列表视图中:

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

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

    【讨论】:

    • 我做了,但没用 :( 有没有办法通过代码做到这一点?
    • @user1429595 有,但需要在活动类中进行。可以提供一下吗?
    • 是的,在您的 rootview 上使用 findviewbyid() 在 oncreateview 中获取对您的列表视图的引用并设置其布局参数(在线查看)
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-30
        • 2011-06-06
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多