所以..我正在尝试更改ViewPager
内的两个视图,每个视图占ViewPager
大小的一半。 ViewPager
本身设置为屏幕大小的75%。
以下是ViewPager
<RelativeLayout
android:id="@+id/activity_main_relativeViewPagerWrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none" >
<android.support.v4.view.ViewPager
android:id="@+id/activity_main_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
以下是ViewPagerAdapter
作为ViewPager
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
int[] title;
int[] message;
int[] background;
int[] image;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, int[] title, int[] message,
int[] background, int[] image) {
this.context = context;
this.title = title;
this.message = message;
this.background = background;
this.image = image;
}
@Override
public int getCount() {
return title.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView titleText;
TextView messageText;
RelativeLayout slidingBackground;
ImageView slidingImage;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
// Locate the TextViews in viewpager_item.xml
titleText = (TextView) itemView.findViewById(R.id.viewpager_item_titleText);
messageText = (TextView) itemView.findViewById(R.id.viewpager_item_messageText);
// Capture position and set to the TextViews
titleText.setText(title[position]);
messageText.setText(message[position]);
// Locate the ImageView in viewpager_item.xml
slidingBackground = (RelativeLayout) itemView.findViewById(R.id.viewpager_item_slidingBackground);
slidingImage = (ImageView) itemView.findViewById(R.id.viewpager_item_slidingImage);
// Capture position and set to the ImageView
slidingBackground.setBackgroundResource(background[position]);
slidingImage.setImageResource(image[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
LayoutResize
上课
public class LayoutResize extends Activity{
public DisplayMetrics displayMetrics = new DisplayMetrics();
public int width(int sizeX, DisplayMetrics displayMetrics)
{
int screenWidth = displayMetrics.widthPixels * sizeX / 100;
return screenWidth;
}
public int height(int sizeY, DisplayMetrics displayMetrics)
{
int screenHeight = displayMetrics.heightPixels * sizeY / 100;
return screenHeight;
}
}
Anddd是我调整布局大小的主要课程
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.activity_main_mainLayout);
//RelativeLayout viewPagerItem = (RelativeLayout)findViewById(R.id.viewPagerItem);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pagerItemInflater = inflater.inflate(R.layout.viewpager_item, mainLayout);
View viewpager_items = inflater.inflate(R.layout.viewpager_item, mainLayout, true);
//setContentView(pagerItemInflater);
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
LayoutResize layoutResize = new LayoutResize();
RelativeLayout relativeViewPager =(RelativeLayout) findViewById(R.id.activity_main_relativeViewPagerWrapper);
int relativeViewPagerHeight = layoutResize.height(75, displayMetrics);
RelativeLayout.LayoutParams viewPagerParams = (RelativeLayout.LayoutParams)relativeViewPager.getLayoutParams();
viewPagerParams.height = relativeViewPagerHeight;
relativeViewPager.setLayoutParams(viewPagerParams);
RelativeLayout slidingBackground =(RelativeLayout) pagerItemInflater.findViewById(R.id.viewpager_item_slidingBackground);
int slidingBackgroundHeight = layoutResize.height(38, displayMetrics);
ViewGroup.LayoutParams slidingBackgroundParams = slidingBackground.getLayoutParams();
slidingBackgroundParams.height = slidingBackgroundHeight;
slidingBackground.setLayoutParams(slidingBackgroundParams);
RelativeLayout slidingImageWrapper = (RelativeLayout) pagerItemInflater.findViewById(R.id.viewpager_item_slidingImageWrapper);
int slidingImageWrapperHeight = layoutResize.height(37, displayMetrics);
RelativeLayout.LayoutParams slidingImageWrapperParams = (RelativeLayout.LayoutParams)slidingImageWrapper.getLayoutParams();
slidingImageWrapperParams.height = slidingImageWrapperHeight;
slidingImageWrapper.setLayoutParams(slidingImageWrapperParams);
slidingImageWrapper
和slidingBackground
都位于ViewPager
内。当我把它们分别放到37%和38%时,它们的大小不会改变。
我在这里做错了吗?如何更改适配器内部的视图?或者甚至不可能?如果不可能,那么..有什么建议吗?
更新:
这是使用Activity
的{{1}}。灰色框上方的所有内容都是ViewPager
,可以在ViewPager
RelativeLayout ViewPager. The grey box takes approximately 20% of screen height, the orange box takes approximately 30% of screen height, and the rest is 50%.
The orange box is made up of
TextView wrapping
TextView . The problem is by wrapping it, the orange box will not have a fixed size. It depends on the
displayMetrics`周围滑动。
更新:
这是我当前的寻呼机适配器
.. I want it to take 30% of the screen using
答案 0 :(得分:0)
我找到了答案!希望有类似问题的人能得到这方面的帮助:
我没有意识到实际上所有内容都在适配器中初始化并在主布局中使用。当您使用其他人的适配器并且只知道如何更改适配器中的值,而不是如何工作
时,该怎么办?所以我在ViewPagerAdapter
RelativeLayout slidingBackground = (RelativeLayout) itemView.findViewById(R.id.viewpager_item_slidingBackground);
然后像......那样做事。
slidingBackground = (RelativeLayout) itemView.findViewById(R.id.viewpager_item_slidingBackground);
int slidingBackgroundHeight = layoutResize.height(30);
ViewGroup.LayoutParams slidingBackgroundParams = slidingBackground.getLayoutParams();
slidingBackgroundParams.height = slidingBackgroundHeight;
在ViewPagerAdapter
所以我无法通过使用ViewPager
的类更改值,但我必须更改适配器本身的值。