Ilario
Ilario

Reputation: 6079

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.

This ScrollView contains a LinearLayout with horizontal orientation, and inside there are 4 RelativeLayout, each of which must fill the screen.

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"

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.

How can I solve the problem? Thanks

Upvotes: 0

Views: 157

Answers (3)

Collins Abitekaniza
Collins Abitekaniza

Reputation: 4588

It seems your RelativeLayout width is set to match_parent of the parent LinearLayout which is0dp.

Try giving your LinearLayout some width

By the way why do you have to use HorizontalScrollView, Use ViewPager instead.More about viewpager here

Example

in your 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

Upvotes: 1

vishal jangid
vishal jangid

Reputation: 3025

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

so use like this layout_width = "200dp"

Upvotes: 0

tiny sunlight
tiny sunlight

Reputation: 6251

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);
}

Upvotes: 0

Related Questions