线性布局不使用所有线条

时间:2017-09-22 21:47:32

标签: android android-layout textview

我想构建一个可以生成多个textview的应用程序,它们都适合指定的布局。 但是一旦该行已满而不是在新行继续,它就不会显示其余的文本视图

我的MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList<String> urls=new ArrayList<String>(); //to read each line
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.sell);
    // Add textview 1
    for (int i=0;i<10;i++){
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams1.setMargins(10, 10, 10, 10);
        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(layoutParams1);
        textView1.setText("TextView1\n2");
        textView1.setBackgroundColor(0xFF3EB427); // hex color 0xAARRGGBB
        textView1.setPadding(10, 10, 10, 10);// in pixels (left, top, right, bottom)
        linearLayout.addView(textView1);

    }
    LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.buy);
    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    for (int i=0;i<2;i++){

        layoutParams2.setMargins(10, 10, 10, 10);
        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(layoutParams2);
        textView1.setText("TextView2");
        textView1.setBackgroundColor(0xFFB60612); // hex color 0xAARRGGBB
        textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
        linearLayout2.addView(textView1);
    }
}
}

我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="@+id/mymain"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ohad.tradertracker.MainActivity"
android:weightSum="1">

<LinearLayout
    android:id="@+id/stats"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="101dp">

</LinearLayout>


<LinearLayout
    android:id="@+id/sell"
    android:layout_weight=".5"
    android:fillViewport="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

它看起来像: enter image description here

如何生成未知数量的textview并使用所有布局大小???

1 个答案:

答案 0 :(得分:0)

我认为你的xml布局存在问题。这样做,告诉我它是否有效。 你可以做两件事:

  1. 只需从主布局中删除 weightSum ,然后按照这样做,并在两个线性布局中提及 layout_weight
  2. 或者你可以提及 weightSum 为2。并且在布局中确保您已将 layout_weight提及为