简体   繁体   English

如何设置LinearLayout(带有7个TextView元素)的宽度(这是Horizo​​ntalScrollView的子级),以便一次只能看到4个?

[英]How to set width of LinearLayout (with 7 TextView Elements), which is a child of HorizontalScrollView, so that only 4 are visible at a time?

I have an LinearLayout (with 7 TextView Elements), within a HorizontalScrollView. 我在Horizo​​ntalScrollView中有一个LinearLayout(带有7个TextView元素)。 The HorizontalScrollView is set to fillViewport. Horizo​​ntalScrollView设置为fillViewport。 I want only 4 TextView elements to be visible at a time. 我希望一次仅显示4个TextView元素。 The user can scroll to view the rest. 用户可以滚动查看其余部分。

Case 1: I am able get the required layout using layout_weight but then I am not able to scroll, as shown in the attached code. 情况1:我可以使用layout_weight获得所需的布局,但随后无法滚动,如随附的代码所示。 I am assuming the scroll doesn't work because weights are computed after GUI renders and so the width of the HorizontalScrollLayout doesn't change. 我假设滚动不起作用,因为权重是在GUI渲染后计算的,因此Horizo​​ntalScrollLayout的宽度不会改变。 Is that right? 那正确吗?

Case 2: If I fix the width, eg "60dp", Then it displays as required and I can scroll as well. 情况2:如果我固定宽度,例如“ 60dp”,则它会根据需要显示,并且我也可以滚动。 However, this wont work on other screen sizes. 但是,这不适用于其他屏幕尺寸。

How can I achieve this effect in a way that it works with different screen sizes. 如何以与不同屏幕尺寸配合使用的方式实现此效果。

Here's the code for Case 1 . 这是案例1的代码。

Layout: 布局:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal" 
        android:weightSum="7">

        <TextView
            style="@style/ViewStyle"
            android:text="1" />

        <TextView
            style="@style/ViewStyle"
            android:text="2" />

        <TextView
            style="@style/ViewStyle"
            android:text="3" />

        <TextView
            style="@style/ViewStyle"
            android:text="4" />

        <TextView
            style="@style/ViewStyle"
            android:text="5" />

        <TextView
            style="@style/ViewStyle"
            android:text="6" />

        <TextView
            style="@style/ViewStyle"
            android:text="7" />
    </LinearLayout>

Style: 样式:

<style name="ViewStyle">
    <item name="android:layout_weight">1</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">60dp</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:gravity">center</item>
    <item name="android:textSize">10sp</item>
    <item name="android:textColor">@color/white</item>
</style>

Using the layout_weight in a LinearLayout wrapped in a HorizontalScrollView will not go well for what you want. HorizontalScrollView包装的LinearLayout使用layout_weight并不能很好地满足您的需求。 I suggest you do something like this: 我建议你做这样的事情:

  1. remove the layout_weight attribute from the style , also modify the layout_width to a value(or you could use wrap_content ) style删除layout_weight属性,还可以将layout_width修改为一个值(或者您可以使用wrap_content
  2. post a Runnable on one of your views in the onCreate method to update the text of the TextViews like this: onCreate方法中的一个视图上发布一个Runnable ,以更新TextViews的文本,如下所示:

     // wrapperLinearLayout being your LinearLayout wrapping the 7 TextViews wrapperLinearLayout.post(new Runnable() { @Override public void run() { // find out the width of the HorizontalScrollView HorizontalScrollView hsv = (HorizontalScrollView) wrapperLinearLayout .getParent(); // the value below will be the new width of all the TextViews so // you can see only for initially int targetWidth = (hsv.getWidth() / 4) * 7; // modify the width of all 7 TextViews for (int i = 0; i < wrapperLinearLayout.getChildCount(); i++) { LinearLayout.LayoutParams lpc = (android.widget.LinearLayout.LayoutParams) wrapperLinearLayout .getChildAt(i).getLayoutParams(); lpc.width = targetWidth / 7; } } }); 

You have to get the screen width at runtime and then set width for your textviews. 您必须在运行时获取屏幕宽度,然后为textview设置宽度。 I suppose that's the only way you can get it to work. 我想那是让它正常工作的唯一方法。

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

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