-1

enter image description here

This view is a representation of a Calendar.

In this layout I have a LineaerLayout inside of an HorizontalScrollView, and inside of LinearLayout they are a lot of TextView. I set min width to TextView, because in portrait position there is not much space, and i don´t want to see 7 super little TextView, I prefer to se 4 TextView mid size.

enter image description here

When I rotate to Landscape position I want to see only 7 TextView with equivalent width and scroll the others, but I can´t to reach that.

Sorry for my bad english. Here is the XML.

<HorizontalScrollView
    android:id="@+id/scroll_dias"
    android:layout_below="@+id/header2"
    android:layout_toRightOf="@+id/tv_etiqueta_hora"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="1dp"
    android:fillViewport="true"
    android:scrollbars="none">


    <LinearLayout
        android:id="@+id/contenedor_dias"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="7">
    </LinearLayout>

</HorizontalScrollView>

And the inflated textview add programmatically to LinearLayout

<TextView
    android:id="@+id/tv_item_dia"
    android:layout_width="60dp"
    android:layout_height="32dp"
    android:layout_marginRight="1dp"
    android:layout_weight="1"
    android:background="#3f7668"
    android:gravity="center"
    android:padding="8dp"
    android:text="Día "
    android:textColor="#fff"
    android:textSize="12dp"
    android:textStyle="bold" />

enter image description here

Oscar Méndez
  • 937
  • 2
  • 13
  • 39
  • If you want to fit a `LinearLayout` with a specific weight into your screen and scroll horizontally for showing 7 `TextView` for each step, why you don't use a `ViewPager` instead of a `HorizontalScrollView`? – andrea.petreri Jan 18 '16 at 19:25
  • I don´t want to show 7 TextView in portrait because there are too small, when I rotate to landscape i want to show only 7, not more or less – Oscar Méndez Jan 18 '16 at 19:32
  • Yes sorry, maybe I was not clear with my question. I was meaning that `ViewPager` could be your container and pages could show `TextViews`. In portrait you will have page with 4 `TextViews` each, while in landscape page will contain 7 `TextViews` in a row. Maybe this is not what you expect, but usage of `HorizontalScrollView` looks strange to me in this case because this would allow you to include just one `LinearLayout` inside (so no more than 4 or 7 `TextViews`), while as far as I understood you want to scroll horizontally for showing other days. – andrea.petreri Jan 18 '16 at 19:39
  • The weighted dimension must measure exactly **0dp**, for weights to work. And `weightSum` is completely optional (it's calculated automatically). – Phantômaxx Jan 18 '16 at 19:52
  • If you have an LinearLayout with weight sum 2, and 3 Textview inside with weight 1, only 2 will show and the other will be off of the screen and can be scrolled. So why mi view doesn´t work? – Oscar Méndez Jan 18 '16 at 19:58
  • @OscarMéndez Having a weightSum equals to 2 means that sum of children weight should not exceed 2. – andrea.petreri Jan 18 '16 at 20:19
  • Ok so talking in % - weight sum 2 = 100% and set weight 1 to a view represents 50%, so I want to have 3 view with 50% width each. 150% in a linear layot, show 100% and scroll the other 50% – Oscar Méndez Jan 18 '16 at 20:26

1 Answers1

1

Have you considered using a GridLayout instead? With GridLayout you can restrict the amount of rows and the amount of columns displayed on the screen at any point of time.

Further to that, you can also use different layouts for different screen widths. You can place your narrow (with 4 columns) layout into the layout folder and you can place your wide layout (with 7 columns) into layout-sw600dp folder. Once you do that Android will automatically pick the correct layout based on the current width of the screen (i.e. portrait vs landscape orientation or tablet vs phone).

Allan Spreys
  • 5,287
  • 5
  • 39
  • 44