好的,很多道歉,我已经阅读了几篇关于如何做到的帖子,但我仍然无法得到我想要的。我有一个Spinner来选择屏幕上显示的内容,我想要一个不同的布局,当Spinner是它的默认位置时,从其他任何东西被选中。 它几乎正常工作但我想在Spinner处于默认位置时获得Globe CENTER_IN_PARENT。我已经尝试以编程方式设置边距来测试是否存在根本错误并且这已经奏效(但未在代码中显示)。
我提前感谢。
看看这两个视图,第一个是Spinner是默认值,第二个是不是: -
我的代码
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// The variable index identifies the position the spinner is in.
// TextView name, country and description... locks to the
// TextViews defined in the activity_main.xml layout files.
int index = parent.getSelectedItemPosition();
TextView name = (TextView) findViewById(R.id.name);
TextView country = (TextView) findViewById(R.id.country);
TextView description = (TextView) findViewById(R.id.description);
TextView link = (TextView) findViewById(R.id.link);
// Used to set up Layout Params for the ImageView, Name and
// Country TextViews
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
.getLayoutParams();
RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams) name
.getLayoutParams();
RelativeLayout.LayoutParams lp3 = (RelativeLayout.LayoutParams) country
.getLayoutParams();
// Now we'll check to see if we're in the None Selected spinner
// position. If true we'll dump the name, country and
// description TextViews otherwise these will be shown.
if (index == 0) {
// Get rid of unwanted views when Spinner is at default
// position
name.setVisibility(View.GONE);
country.setVisibility(View.GONE);
description.setVisibility(View.GONE);
link.setVisibility(View.GONE);
// Try and centre the image when Spinner in default position
// The first line acquires the image, the other lines try
// and centre the image by 1st clearing ALIGN_PARENT_LEFT
// and then setting CENTER_IN_PARENT
image.setImageResource(imgs.getResourceId(
spinner.getSelectedItemPosition(), -1));
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
lp.addRule(RelativeLayout.CENTER_IN_PARENT,
RelativeLayout.TRUE);
image.setLayoutParams(lp);
} else {
// Spinner is not in its default position so lay the view
// out different by ALIGN_PARENT_LEFT
image.setImageResource(imgs.getResourceId(
spinner.getSelectedItemPosition(), -1));
lp.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
RelativeLayout.TRUE);
image.setLayoutParams(lp);
// Now set up rules for other TextViews and make them VISIBLE
lp2.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp3.addRule(RelativeLayout.CENTER_HORIZONTAL);
name.setVisibility(View.VISIBLE);
country.setVisibility(View.VISIBLE);
description.setVisibility(View.VISIBLE);
link.setVisibility(View.VISIBLE);
name.setText(leaders[index]);
country.setText(states[index]);
description.setText(descrip[index]);
// Do some formatting for the hyperlinks
link.setText(links[index]);
link.setText(Html.fromHtml(links[index]));
link.setLinksClickable(false);
link.setMovementMethod(LinkMovementMethod.getInstance());
}
}
答案 0 :(得分:0)
抱歉,无法在评论中发帖。
您的布局似乎缩小到下图
中白框的大小要解决此问题,您应该将父级布局的height
和width
从wrap_content
更改为match_parent
在布局文件中执行以下操作
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
答案 1 :(得分:0)
这对我有效:
Google Chrome 84.0.4147.105 (Official Build) (64-bit)
Revision a6b12dfad6663f13a7e16e9a42a6a4975374096b-refs/branch-heads/4147@{#943}
JavaScript V8 8.4.371.22