LinearLayout使用半屏

时间:2017-03-03 18:31:48

标签: android android-layout

您好我有一个ScrollView和一个垂直LinearLayout。在每个LinearLayout广告位中,我有一个水平LinearLayout,其中包含一个单词和一个数字。问题是隐藏的一些词?它占据了屏幕的一半。谢谢你的帮助。

Layout bounds

Problem

for (int i = 0; i < words.size(); i++) {
    LinearLayout horizontal = new LinearLayout(context);
    horizontal.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    horizontal.setLayoutParams(LLParams);

    btnWord.add(i, new Button(context));
    btnWord.get(i).setHeight(60);
    btnWord.get(i).setWidth(120);
    btnWord.get(i).setTypeface(montFont);
    btnWord.get(i).setBackgroundColor(Color.WHITE);
    btnWord.get(i).setTag(i);
    btnWord.get(i).setGravity(Gravity.CENTER);
    btnWord.get(i).setText("  " + words.get(i));
    btnWord.get(i).setOnClickListener(btnClicked);
    horizontal.addView(btnWord.get(i));

    wordWeight.add(i, new Button(context));
    wordWeight.get(i).setHeight(60);
    wordWeight.get(i).setWidth(40);
    wordWeight.get(i).setTypeface(montFont);
    wordWeight.get(i).setBackgroundColor(Color.WHITE);
    wordWeight.get(i).setTag(i);
    wordWeight.get(i).setGravity(Gravity.CENTER);
    wordWeight.get(i).setText("  " + wordWeights.get(i));
    wordWeight.get(i).setOnClickListener(btnClicked);
    horizontal.addView(wordWeight.get(i));

    linearLayout.addView(horizontal);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">


    <ScrollView
        android:layout_width="match_parent"
        style="@android:style/Widget.ScrollView"
        android:layout_marginTop="106dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearLayout" />

    </ScrollView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

您可以尝试设置每个&#34;项目的weight&#34;到1,所以他们将平均划分屏幕上的空间。类似下面的代码片段:

LinearLayout ll;
LinearLayout.LayoutParams lp;
lp.weight = 1;
ll.setLayoutParams(lp);

您也可以抛弃该逻辑,并使用ListViewthis answer上的自定义适配器一样,或者设置RecyclerView,如this blog post所示。这样做的任何一种方法都更简单,更有效。

有关ListViewRecyclerView here的更多信息。