Android 片段内容被切断
我目前正在开发一款Android游戏。我在片段中的内容有问题。内容被切断了,我找不到问题所在。我只使用资源目录,在那里我定义了不同的布局。这就是为什么我知道问题在于布局。首先,我认为这与我使用的是android:layout\u height=“match\u parent”有关。我在StackOverflow上找到了这个解决方案,我希望这也能解决我的问题。但这对我毫无帮助 我有Android 片段内容被切断,android,android-layout,Android,Android Layout,我目前正在开发一款Android游戏。我在片段中的内容有问题。内容被切断了,我找不到问题所在。我只使用资源目录,在那里我定义了不同的布局。这就是为什么我知道问题在于布局。首先,我认为这与我使用的是android:layout\u height=“match\u parent”有关。我在StackOverflow上找到了这个解决方案,我希望这也能解决我的问题。但这对我毫无帮助 我有活动\u game.xml。这里有一个占位符,用于当前显示的片段。内容如下: <?xml version="1.
活动\u game.xml
。这里有一个占位符,用于当前显示的片段
。内容如下:
<?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>
这是显示问题的捕获屏幕
如您所见,最后两个按钮正在被剪切。我希望避免这种情况,而是允许用户向下滚动。我身陷绝境,如果能得到任何帮助,我将不胜感激。我希望我能够很好地解释这个问题,让大家理解。您应该将片段布局包装在一个文件中,以启用滚动:
<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>
您可以在此处看到一个使用中的示例:不使用
滚动视图
,您可以为列表视图
设置权重。尝试以下解决方案:
<ListView
...
android:layout_weight="1"/>
您应该使父容器的高度与父容器的高度相匹配
<?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>
我会避免将整个布局包装在滚动视图中,因为它包含ListView,应该避免嵌套滚动视图。相反,如果希望按钮显示在列表的底部,可以将其作为ListView中的最后一个列表项。要做到这一点,您必须在适配器中重写和
另一种解决方案是将按钮放在ListView的顶部,方法是将ListView和TableView放在相对位置,然后最后声明TableView。如果布局已经包含ListView,则将父布局应用为scrollview不是一种好的做法。但是,如果您仍然想使用它,那么您可以动态设置listview的高度,以便在不滚动的情况下显示其所有项目。您可以通过使用这个小代码来实现这一点
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();
}
});
}
希望能有帮助。快乐编码:)我马上就试试。我应该在片段布局的LinearLayout
中执行此操作,还是建议将LinearLayout
替换为ScrollView
?好的,我想我明白了您的解决方案。我必须将线性布局
放在滚动视图
中。是的,通常您会将线性布局
放在滚动视图
中。我没有解决问题。通过使用此选项,我自动缩小了用于显示您可以销售的所有药物列表的列表视图。现在列表只显示标题列,列表中有滚动条。我一次只能看到一行。ListView
本身现在可以滚动吗?我会试试看,因为ScrollView
没有起作用。对不起,我更新了我的答案ListView
而不是TableRow
OK,我使用了这个,现在ListView
有一个滚动条。这样,内容就完全显示出来了。我想滚动整个内容,但这个解决方案也足够好。谢谢如何/为什么这样做?为什么要特别将其添加到ListView?我也有同样的问题,但在线性布局中的片段中只有一堆微调器和编辑文本,这对我没有帮助。我在这里开始提问之前已经试过了。无论如何,谢谢你。
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 yourListView = (ListView) findViewById(R.id.your_list_view);
YourListViewAdapter adapter = new YourListViewAdapter();
yourListView.setAdapter(adapter);
//set height here
setListViewHeightBasedOnChildren(yourListView);