2012-07-31 86 views
9

嗨,大家好我有一个空的线性布局在XML中。我将动态添加一些文本视图。线性布局内线性布局正在添加,但不可见

一旦这些textviews超过5,然后我创建一个更多的线性布局,并添加文本视图到新创建的布局。最后将这个新的布局添加到主布局。

我可以添加,即在模拟器占据该空间,但不会在textviews中显示信息。

我的XML如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/dyn_layout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:padding="10dip" > 
     </LinearLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/dyn_button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add TextViews" /> 

</LinearLayout> 

和我的Java文件如下:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class AddTextViewsDynamically extends Activity implements 
     OnClickListener { 

    Button button; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_textview_dynamically); 

     button = (Button) findViewById(R.id.dyn_button1); 
     button.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.dyn_button1: 
      LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout); 
      LinearLayout layout2 = null; 
      LinearLayout layout3 = null; 
      for (int i = 0; i < 10; i++) { 
       if (i > 4) { 
        if (i == 5) { 
         layout2 = new LinearLayout(this); 
         layout2.setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 
         layout2.setOrientation(LinearLayout.VERTICAL); 
         layout2.setPadding(10, 60, 10, 10); 
         layout3 = new LinearLayout(this); 
         layout3.setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 
         layout3.setOrientation(LinearLayout.HORIZONTAL); 
         layout3.setPadding(10, 10, 10, 10); 
        } 
        System.out.println("**** Adding text view " + i); 
        TextView text = new TextView(this); 
        text.setText("The Value of i is :" + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        text.setLayoutParams(new LayoutParams(155, 
          LayoutParams.WRAP_CONTENT)); 
        layout3.addView(text); 

        if (i == 9) { 
         System.out 
           .println("Added second linear layout to first"); 
         layout2.setVisibility(View.VISIBLE); 
         layout2.addView(layout3); 
         layout.addView(layout2); 
        } 
       } else { 
        System.out.println("###### Adding text view " + i); 
        TextView text = new TextView(this); 
        text.setText("The Value of i is :" + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        text.setLayoutParams(new LayoutParams(155, 
          LayoutParams.WRAP_CONTENT)); 
        layout.addView(text); 
       } 
      } 

     } 

    } 

} 

可以在任何一个电话我,我要去哪里错了。请帮助我。

预先感谢您 Mouni

+0

你能告诉你为什么这样做,因此只要能提供交替和最佳的解决方案? – 2012-07-31 07:11:27

+0

我将从服务中动态获取一些信息,这些信息必须在每行的文本视图5中显示。如果它更接近下一行。 – Mouni 2012-07-31 07:24:04

+0

有没有其他办法可以做到这一点.. ??请让我知道 – Mouni 2012-07-31 07:24:46

回答

5

您的主要的LinearLayout被设置为水平,从而所述第一5文本视图和布局2在相同的线表示。将Layout3添加到Layout2会使Layout3显示在主Linear Layout的最后一个文本视图的右侧。在10英寸平板电脑上,我只看到LinearLayout的前2个元素。也许在更小的屏幕上你看不到它们。尝试使用的

text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT)); 

代替

text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT)); 

,你应该看到您所有的文本视图。

编辑: 在你的情况下,这应该做的伎俩; XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/dyn_layout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:padding="10dip" > 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/dyn_layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:visibility="gone" 
      android:padding="10dip" > 
     </LinearLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/dyn_button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add TextViews" /> 

</LinearLayout> 

和代码:

@Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.dyn_button1: 
      LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout); 
      LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2); 
      LinearLayout layout3 = null; 
      for (int i = 0; i < 10; i++) { 
       if (i > 4) { 
        if (i == 5) { 
         layout2.setPadding(10, 60, 10, 10); 
         layout3 = new LinearLayout(this); 
         layout3.setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 
         layout3.setOrientation(LinearLayout.HORIZONTAL); 
         layout3.setPadding(10, 10, 10, 10); 
        } 
        System.out.println("**** Adding text view " + i); 
        TextView text = new TextView(this); 
        text.setText("The Value of i is :" + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        text.setLayoutParams(new LayoutParams(155, 
          LayoutParams.WRAP_CONTENT)); 
        layout3.addView(text); 

        if (i == 9) { 
         System.out 
           .println("Added second linear layout to first"); 
         layout2.setVisibility(View.VISIBLE); 
         layout2.addView(layout3); 
        } 
       } else { 
        System.out.println("###### Adding text view " + i); 
        TextView text = new TextView(this); 
        text.setText("The Value of i is :" + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        text.setLayoutParams(new LayoutParams(155, 
          LayoutParams.WRAP_CONTENT)); 
        layout.addView(text); 
       } 
      } 

     } 

    } 
+0

非常感谢你。是的,所有10个文字浏览都是垂直添加的。但是我只想要一行5行,接下来5行我想这样做的布局是什么?..? plz帮助我.. – Mouni 2012-07-31 10:05:18

+0

是的,它的工作,但有点小的变化,因为我不会在xml中添加布局..它将被动态添加..现在它的工作正常.. :)再次谢谢你.. – Mouni 2012-07-31 10:26:10

0

试试这一个,然后你会看到你的代码就可以了。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="100" > 

<HorizontalScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="80" > 

    <LinearLayout 
     android:id="@+id/dyn_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="10dip" > 
    </LinearLayout> 
</HorizontalScrollView> 

<Button 
    android:id="@+id/dyn_button1" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="20" 
    android:text="Add TextViews" /> 

</LinearLayout> 
+0

努力工作出来.. :(差异只是按钮即将在屏幕的结尾..有什么我必须改变.. ?? – Mouni 2012-07-31 07:27:52

+0

看到我编辑的答案,如果你想水平添加文本视图,你需要使用Horizo​​ntalScrollView而不是常规的。并让我知道,如果它的工作,(顺便说一句,我试了一下,它与我合作) – osayilgan 2012-07-31 07:50:46

+0

感谢您的答复。即使现在没有改变与以前一样.. – Mouni 2012-07-31 08:17:45

0

其实你正在做的事情是正确的,我只是删除填充,并将所有线性布局的方向设置为垂直。在xml文件上修改它也会起作用:)))它只是你添加了一个10或60的填充,这个填充是父视图的视图。

改进的Java代码:

@Override 
public void onClick(View v) { 

switch (v.getId()) { 
    case R.id.dyn_button1: 
     LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout); 
     LinearLayout layout2 = null; 
     LinearLayout layout3 = null; 
     for (int i = 0; i < 10; i++) { 
      if (i > 4) { 
       if (i == 5) { 
        layout2 = new LinearLayout(this); 
        layout2.setLayoutParams(new LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
        layout2.setOrientation(LinearLayout.VERTICAL); 
       // layout2.setPadding(10, 10, 10, 10); 
        layout3 = new LinearLayout(this); 
        layout3.setLayoutParams(new LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
        layout3.setOrientation(LinearLayout.VERTICAL); 
       //  layout3.setPadding(10, 10, 10, 10); 
       } 
       System.out.println("**** Adding text view " + i); 
       TextView text = new TextView(this); 
       text.setText("The Value of i is :" + i); 
       text.setTextSize(12); 
       text.setGravity(Gravity.LEFT); 
       text.setLayoutParams(new LayoutParams(155, 
         LayoutParams.WRAP_CONTENT)); 
       layout3.addView(text); 

       if (i == 9) { 
        System.out 
          .println("Added second linear layout to first"); 
        layout2.setVisibility(View.VISIBLE); 
        layout2.addView(layout3); 
        layout.addView(layout2); 
       } 
      } else { 
       System.out.println("###### Adding text view " + i); 
       TextView text = new TextView(this); 
       text.setText("The Value of i is :" + i); 
       text.setTextSize(12); 
       text.setGravity(Gravity.LEFT); 
       text.setLayoutParams(new LayoutParams(155, 
         LayoutParams.WRAP_CONTENT)); 
       layout.addView(text); 


      } 
     } 

    } 

} 
+0

你好阿布西,你是收到两行信息还是只是一..? – Mouni 2012-07-31 08:15:14

+0

是的,它与垂直布局正常工作..但我希望它在水平布局,即每行5个值..我应该做的是..? – Mouni 2012-07-31 08:16:57