Android - 更改viewpager中视图的大小

时间:2016-01-11 06:07:25

标签: android android-viewpager

所以..我正在尝试更改ViewPager内的两个视图,每个视图占ViewPager大小的一半。 ViewPager本身设置为屏幕大小的75%。

以下是ViewPager

的xml代码的一部分
<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);

slidingImageWrapperslidingBackground都位于ViewPager内。当我把它们分别放到37%和38%时,它们的大小不会改变。

我在这里做错了吗?如何更改适配器内部的视图?或者甚至不可能?如果不可能,那么..有什么建议吗?

更新: ViewPager 这是使用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

1 个答案:

答案 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的类更改值,但我必须更改适配器本身的值。