简体   繁体   English

具有四个RelativeLayout的Horizo​​ntalScrollView,每个RelativeLayout占据整个屏幕

[英]HorizontalScrollView with four RelativeLayout, each of which occupies the entire screen

I am having a problem creating a HorizontalScrollView on Android that will serve as a tutorial. 我在Android上创建HorizontalScrollView时遇到问题,该教程将作为教程。

This ScrollView contains a LinearLayout with horizontal orientation, and inside there are 4 RelativeLayout, each of which must fill the screen. 此ScrollView包含一个水平方向的LinearLayout,内部有4个RelativeLayout,每个必须填满屏幕。

But if I set layout_width = "match_parent" on each RelativeLayout, this does not work at all, but it's like it was set to "wrap_content" 但是,如果我在每个RelativeLayout上设置layout_width = "match_parent" ,则根本不起作用,但这就像它被设置为"wrap_content"

The layout_width of ScrollView is set as "wrap_content" and on LinearLayout is set as "0dp" , but changing this I did not see any changes. ScrollView的layout_width设置为"wrap_content" ,而LinearLayout上的设置为"0dp" ,但是更改此设置后,我看不到任何更改。

How can I solve the problem? 我该如何解决这个问题? Thanks 谢谢

It seems your RelativeLayout width is set to match_parent of the parent LinearLayout which is 0dp . 看来您的RelativeLayout宽度设置为父LinearLayout的match_parent ,即0dp

Try giving your LinearLayout some width 尝试为您的LinearLayout设置一些宽度

By the way why do you have to use HorizontalScrollView , Use ViewPager instead.More about viewpager here 顺便一句,为什么必须使用HorizontalScrollView ,而改为使用ViewPager 。有关viewpager的更多信息 ,请ViewPager 此处。

Example

in your parent_layout.xml 在您的parent_layout.xml中

   <android.support.v4.view.ViewPager
     android:id="@+id/viewpager"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />

You can find more about the code at GitHub 您可以在GitHub上找到有关代码的更多信息

int  size = horizontalScrollView.getChildCount();
int screenW =getResources().getDisplayMetrics().widthPixels;
for(int i = 0 ;i <size ;i++){
     View v = horizontalScrollView.getChildAt(i);
    ViewGroup.LayoutParams lp = v.getLayoutParams();
    lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
    lp.width = screenW;
    v.setLayoutParams(lp);
}

when you use the scroll view you should use the width of Relative layout in fix dp like 300dp or 200 dp otherwise scroll view take its width as it require like wrap_content 当您使用滚动视图时,应在fix dp中使用相对布局的宽度,例如300dp或200 dp,否则滚动视图将其宽度作为需要的宽度,例如wrap_content

so use like this layout_width = "200dp" 所以使用像这样layout_width = "200dp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM