0

Horizo​​ntalScrollView 内に LinearLayout (7 つの TextView 要素を持つ) があります。Horizo​​ntalScrollView は、fillViewport に設定されています。一度に表示できる TextView 要素は 4 つだけです。ユーザーはスクロールして残りを表示できます。

ケース 1: 添付のコードに示すように、layout_weight を使用して必要なレイアウトを取得できますが、スクロールできません。GUI レンダリング後に重みが計算され、Horizo​​ntalScrollLayout の幅が変わらないため、スクロールが機能しないと想定しています。そうですか?

ケース 2: 「60dp」などの幅を固定すると、必要に応じて表示され、スクロールすることもできます。ただし、これは他の画面サイズでは機能しません。

さまざまな画面サイズで機能する方法でこの効果を実現するにはどうすればよいですか。

Case 1のコードは次のとおりです。

レイアウト:

    <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 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>
4

2 に答える 2

2

layout_weighta でLinearLayoutラップされたaを使用すると、HorizontalScrollView必要なものにはうまくいきません。次のようなことをお勧めします。

  1. layout_weightから属性を削除し、 を値にstyle変更しlayout_widthます (または を使用できますwrap_content) 。
  2. Runnableメソッド内のビューの 1 つに投稿して、次のようなonCreateテキストを更新します。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;
            }
        }
    });
    
于 2013-01-16T08:44:22.740 に答える
0

実行時に画面の幅を取得してから、テキストビューの幅を設定する必要があります。それがあなたがそれを機能させることができる唯一の方法だと思います。

于 2013-01-15T16:24:17.900 に答える