0

ビュー幅が固定されている場合に、一度に画面に表示されるビューの量を計算したいと思います。そのために、1つのレイアウトを取得し、固定サイズでいくつかのビューを追加して実行します。

しかし、私の計算によると、画面に表示されるのと同じ数の子が画面に表示されません。

どこが間違っているのか教えてください。

これが私のコードです...

   In Activity ...
    ----
     LinearLayout featured_listlayout_horizontallayout=(LinearLayout)findViewById(R.id.featured_listlayout_horizontallayout);
            LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
            for(int i=0;i<20;i++){
                LinearLayout childItem=(LinearLayout)inflater.inflate(R.layout.childitemlayout,null);
                Button btn=(Button)childItem.findViewById(R.id.btn);
                btn.setText("Item"+(i+1));
                featured_listlayout_horizontallayout.addView(childItem);
            }

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    final int height = dm.heightPixels;
    float screenWidth = dm.widthPixels;//Screen Width in pixel

    float itemWidth=getResources().getDimension(R.dimen.featured_text);//itemWidth in DP
    itemWidth=convertDpToPixel(itemWidth, getApplicationContext());// convert itemWidth into pixel

    System.out.println("screenWidth "+screenWidth+" itemWidth "+itemWidth);

    float noOfItem=screenWidth/itemWidth;
    System.out.println("noOfItem "+noOfItem);

    -----

    convertPixelsToDp method:


    public float convertPixelsToDp(float px,Context context){
            Resources resources = context.getResources();
            DisplayMetrics metrics = resources.getDisplayMetrics();
            float dp = px / (metrics.densityDpi / 160f);
            return dp;
        }  

  convertDpToPixel method:

   public float convertDpToPixel(float dp,Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi/160f);
        return px;
    }

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/featured_listlayout_horizontallayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="5dp" >
            </LinearLayout>
        </HorizontalScrollView>

    </RelativeLayout>


    childitemlayout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="@dimen/featured_text"
        android:layout_height="@dimen/featured_image"
        android:orientation="vertical" 
        android:background="#0000ff">

        <Button android:id="@+id/btn"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Button"
                android:background="#ff00ff"/>


    </LinearLayout>



    dimen.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
             <dimen name="featured_text">80dp</dimen>
             <dimen name="featured_image">60dp</dimen>
    </resources>
4

1 に答える 1

1

しかし、私の計算によると、画面に表示されるのと同じ数の子が画面に表示されません。

画面の幅をピクセル単位で取得します。

float screenWidth = dm.widthPixels;

アイテムの幅については、ピクセル単位の幅を取得しますが、次のように変換しますdp

float itemWidth=getResources().getDimension(R.dimen.featured_text);
itemWidth=convertPixelsToDp(itemWidth, getApplicationContext());

次に、これら2つの値を一緒に使用しようとします。

pixelsまたはを使用しますdp

編集2:

あなたは本当にクラスのinflate()メソッドについて読むべきです。LayoutInflaterビューを適切に膨らませる必要があります。これは、膨らませたレイアウトファイルの親となるをLayoutParams提供することによって行われます。ViewGroupだからあなたはする必要があります:

LinearLayout childItem = (LinearLayout) inflater.inflate(
                    R.layout.aaaaaaaaaaaa, featured_listlayout_horizontallayout, false);

これを行わないと、膨らんだビューに設定されたサイズが設定されません(おそらく、コンテンツがラップされるのを見たことがあるでしょう)。上記の変更を行った後、(xmlレイアウトで設定した寸法を使用して)実際にレイアウトに追加する前に、(完全にまたは部分的に)表示される子の数を確認したい場合は、画面幅からリソースからの寸法まで:

int screenWidth = dm.widthPixels;
int itemWidth=getResources().getDimension(R.dimen.featured_text);
int noOfItem=screenWidth/itemWidth;
// if we have a remainder from the division it means there is extra space
// so we need to check if we have more items so there is another child partially showing
int rem = screenWidth % itemWidth;
if (rem != 0) {
    noOfItem += 1;
}

編集1:

あなたの現在のコードは機能せず、あなたは私の答えを理解していませんでした。dm.widthPixels画面幅(HorizontalScrollView塗りつぶし)をピクセル単位で返します。getResources().getDimension(R.dimen.featured_text)そのディメンションリソースの値をピクセル単位で返します。これらの値を変換するメソッドは必要ありません。直接使用するだけです。LayoutParamsそれでも、ビューに適切なものを割り当てる必要がないため、コードは期待どおりに機能しません。次のように子レイアウトを膨らませる必要があります。

LinearLayout childItem = (LinearLayout) inflater.inflate(
                    R.layout.aaaaaaaaaaaa, featured_listlayout_horizontallayout, false);

また、以下のコード(onCreateメソッドで使用)を使用して、画面に表示されている子(完全に表示または部分的に表示)を見つけます。

featured_listlayout_horizontallayout.post(new Runnable() {

        @Override
        public void run() {
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            // screen width
            final float screenWidth = dm.widthPixels;
            final int count = featured_listlayout_horizontallayout
                    .getChildCount();
            int realWidth = 0;
            int visibleChildren = 0;
            for (int i = 0; i < count; i++) {
                final View child = featured_listlayout_horizontallayout
                        .getChildAt(i);
                realWidth += child.getWidth();
                if (realWidth < screenWidth) {
                    visibleChildren++;
                } else {
                    visibleChildren++;
                    break;
                }
            }
            // visibleChildren now has the visible children on the screen
        }

    });
于 2013-01-11T14:29:27.673 に答える