13

単一の線形レイアウトを 2 つの列 (新聞の列のように) に分割する必要があります。線形レイアウトには、テキスト ビューイメージビューが含まれます。

画面の幅を半分に分割し、TextViewandImageViewを最初の列、つまり下の図のブロックに入るようにしましA B C た。残りのTextViewandImageViewは次の列に移動する必要があります。 D E F.だから、誰かがこれを実装するためのコードやアイデアを私に与えてくれると助かります..私はGridView自分の問題に適していないものを試しました. サイズは明確ではないためTextViewImageView

ここに画像の説明を入力

ライナーレイアウトの分割方法がわかりません。このようにルートレイアウトの高さを計算してみました

linearLayout.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                int linsize=linearLayout.getHeight();
                int relsize=root.getHeight();
                int textsize=txt1.getHeight();
                mainheight=relsize;
                subheight=linsize;
                Toast.makeText(getApplicationContext(), "Linerlayout "+linsize, Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "Relative layout"+relsize, Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "text height "+textsize, Toast.LENGTH_LONG).show();

                if(mainheight==subheight)
                {
                    Toast.makeText(getApplicationContext(), "make a new linear layout", Toast.LENGTH_LONG).show();
                    createsubview();
                }
            }
        }); 

スクリーンショット

ここに画像の説明を入力

4

3 に答える 3

8

ネストされた でこれを簡単に行うことができますLinearLayouts

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/item" />

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                content here/>

            <TextView
                content here/>

        </LinearLayout>
    </LinearLayout>

次に、A、B、C を最初の垂直レイアウトに配置し、D、E、F を 2 番目のレイアウトに配置するだけです。

于 2013-03-07T11:45:47.350 に答える
1

GridView ではできません。これを行うには、カスタム ビューを作成する必要があります。

グリッド アイテムの大きさがわかっている場合は、いくつかのコーナーをカットできます。GridView は、あらゆるサイズのアイテムを処理して動的にロードするため、複雑です。あなたにとってより簡単な方法は次のとおりです。

1.内部に水平 LinearLayout を含む Horizo​​ntalScrollView を作成します。

2.画面に収まるアイテムの行数を決定します。これを行と呼びます。

3. レイアウトする必要がある項目がまだある場合:

    1.Create a vertical LinearLayout, adding rows or less items to it.
    2.Add your new vertical LinearLayout to the horizontal one.

「水平 GridView」で得られるものと比較して、いくつかの欠点があります。

1.All the views are loaded up immediately, which is bad for huge lists of items.
2.You need to know how big your items are, and they need to be the same size.

利点:

1.It's very easy to implement.

詳細については、このリンクを参照してください

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView scrollView = new ScrollView(this);//ScrollView
    LinearLayout ll = new LinearLayout(this); //root LinearLayout
    ll.setOrientation(LinearLayout.HORIZONTAL);//with horizontal orientation
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1f);
    LinearLayout l2 = new LinearLayout(this); //sub linearlayout
    l2.setOrientation(LinearLayout.VERTICAL);//with vertical orientation
    l2.setLayoutParams(layoutParams);
    LinearLayout l3 = new LinearLayout(this); //sub linearlayout
    l3.setOrientation(LinearLayout.VERTICAL);//with vertical orientation
    l3.setLayoutParams(layoutParams);
    int totalvalues=41;     //i take count as 41
    for(int i=0;i<totalvalues;i++){  // add the buttons in the layout based on condition
        Button okButton=new Button(this);
        okButton.setText("Button"+i);
        if(i<=totalvalues/2){
        l2.addView(okButton);
        }
        else{
        l3.addView(okButton);   
        }
    }
    ll.addView(l2);   //add sub linearlayout to root linearlayout
    ll.addView(l3);  //add sub linearlayout to root linearlayout

    scrollView.addView(ll); //add the root linearlayout to scrollview

    setContentView(scrollView);


}
于 2013-03-07T11:45:55.147 に答える
0

やってみました:

DisplayMetrics metrics = getResources().getDisplayMetrics();
float dpW = 0f;
int pixelsW = (int) (metrics.density * dpW + 0.5f);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(pixelsW, LayoutParams.WRAP_CONTENT, 1f);
TextView txt = new TextView(MainActivity.this);
ImageView img = new ImageView(MainActivity.this);
txt.setLayoutParams(lp);
img.setLayoutParams(lp);

TableLayout の LayoutParams を使用すると、ビューの重みを設定できますが、ご存知のとおり、これは 1 でなければなりません。また、DisplayMetrics を使用して、フロートを xml で使用される「dp」形式に変換します。

編集:

この LayoutParams を LinearLayout に設定することもできます。

于 2015-03-03T09:23:41.130 に答える