【问题标题】:Fragment content gets cut off片段内容被截断
【发布时间】:2016-04-19 02:46:16
【问题描述】:

我目前正在开发一款 Android 游戏。我的片段内的内容有问题。内容被切断,我无法找到问题。我只使用资源目录,在这里我定义了不同的布局。这就是为什么我知道问题在于布局。首先,我认为这与我使用 android:layout_height="match_parent" 的事实有关。我在 StackOverflow 上找到了这个解决方案,我希望这也能解决我的问题。但这对我一点帮助都没有。

我有activity_game.xml。我有一个占位符用于当前显示的Fragment。内容如下:

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="y.trader.com.rhcloud.blackmonster.games.tradery.activity.GameActivity">

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

</RelativeLayout>

Fragment 本身定义了以下布局:

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

  <TextView
    android:id="@+id/money"
    style="@style/traderY.TextCenter" />

  <TextView
    android:id="@+id/location"
    style="@style/traderY.TextCenter" />

  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/junkie_icon"/>

  <ListView
    android:id="@+id/drug_list"
    style="@style/traderY.List" />

  <TableRow
    style="@style/traderY.ButtonRow">

    <Button
        android:id="@+id/sell_drugs"
        style="@style/traderY.ButtonCenter"
        android:text="@string/sell"/>

    <Button
        android:id="@+id/nothing"
        style="@style/traderY.ButtonCenter"
        android:text="@string/nothing"/>

  </TableRow>

</LinearLayout>

这是显示问题的捕获屏幕。

如您所见,最后两个按钮被剪掉了。我想避免这种情况,而是允许用户向下滚动。我处于死亡状态,我将不胜感激。我希望我能够很好地解释这个问题,以便人们理解。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    不使用ScrollView,您可以将weight 设置为ListView。试试这个解决方案:

     <ListView
        ...
        android:layout_weight="1"/>
    

    【讨论】:

    • 我会试一试,因为ScrollView 没有成功。
    • 对不起,我更新了我的答案。 ListView 而不是 TableRow
    • 好的,我用过这个,现在ListView 有一个滚动条。这样内容就可以完全显示出来。我想滚动整个内容,但这个解决方案也足够好。谢谢!
    • 如何/为什么这样做?为什么要专门将其添加到 ListView 中?我遇到了同样的问题,但线性布局内的片段中只有一堆 Spinners 和 EditTexts
    【解决方案2】:

    您应该将片段布局包装在 Scrollview 中以启用滚动:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
    <!-- your layout here -->
    
    </ScrollView>
    

    您可以在此处查看它的使用示例:Android ScrollView Example

    【讨论】:

    • 我马上试试。我应该在 Fragment 布局的 LinearLayout 内执行此操作,还是建议将 LinearLayout 替换为 ScrollView?
    • 好的,我想我得到了你的解决方案。我必须将LinearLayout 放在ScrollView 内。
    • 是的,通常您会将LinearLayout 放在ScrollView 中
    • 我没有解决我的问题。通过使用它,我自动缩小了ListView,用于显示您可以出售的所有药物的列表。现在列表只显示标题列,我在列表中滚动。我一次只能看到一行。
    • ListView 本身现在可以滚动吗?
    【解决方案3】:

    你应该让你的父容器高度匹配_parent

    <?xml version="1.0" encoding="utf-8"?>
    <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:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="y.trader.com.rhcloud.blackmonster.games.tradery.activity.GameActivity">
    
      <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    【讨论】:

    • 这对我没有帮助。在我在这里打开问题之前,我已经尝试过了。无论如何,谢谢。
    【解决方案4】:

    我会避免将整个布局包装在 ScrollView 中,因为它包含 ListView 并且应该避免嵌套滚动视图。相反,如果您希望按钮出现在列表底部,您可以将它们设置为 ListView 中的最后一个列表项。您必须在适配器中覆盖 getItemViewType(int position) 和 getViewTypeCount() 才能执行此操作。

    另一种解决方案是将按钮放在 ListView 的顶部,方法是将 ListView 和 TableView 放在一个 RelativeLayout 中,然后最后声明 TableView。

    【讨论】:

      【解决方案5】:

      如果您的布局已经包含一个列表视图,那么将父布局应用为滚动视图不是一个好习惯。但是,如果您仍想使用它,则可以动态设置列表视图的高度,以使其所有项目可见而无需滚动。你可以通过使用这个小代码来实现这一点

          public static void setListViewHeightBasedOnChildren(final ListView listView) {
                  listView.post(new Runnable() {
                      @Override
                      public void run() {
                          ListAdapter listAdapter = listView.getAdapter();
                          if (listAdapter == null) {
                              return;
                          }
                          int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
                          int listWidth = listView.getMeasuredWidth();
                  for (int i = 0; i < listAdapter.getCount(); i++) {
                              View listItem = listAdapter.getView(i, null, listView);
                              listItem.measure(
                                      View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
                              View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
      
      
                              totalHeight += listItem.getMeasuredHeight();
                              Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
                          }
      
                          Log.d("totalHeight " + totalHeight, "********");
      
                          ViewGroup.LayoutParams params = listView.getLayoutParams();
                          params.height = (int) ((totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))));
                          listView.setLayoutParams(params);
                          listView.requestLayout();
      
                      }
                  });
              }
      

      只需将您的 ListView Id 传递给此方法,它就会为您动态设置列表视图高度。然后您就可以毫无问题地使用滚动视图,并根据需要滚动整个内容。

      用法:

          ListView yourListView = (ListView) findViewById(R.id.your_list_view);
      
          YourListViewAdapter adapter = new YourListViewAdapter();
      
          yourListView.setAdapter(adapter);
      
          //set height here
          setListViewHeightBasedOnChildren(yourListView);
      

      希望对您有所帮助。快乐编码:)

      【讨论】:

        猜你喜欢
        • 2017-03-23
        • 1970-01-01
        • 2017-08-31
        • 2014-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-22
        • 2013-07-10
        相关资源
        最近更新 更多