4

我正在做一个类似whatsup的应用程序。我的问题是关于界面的。我可以得到一条消息 desc 并解析它。之后我可以以编程方式添加布局,但它一直在写相同的坐标。我尝试tv.layout(l, t, r, b)了 tv.setTop 但不起作用。

我的问题在哪里?

  bubbleLayout=(RelativeLayout)findViewById(R.id.layoutbubble);

  for(int i=0;i<msgid.length;i++){
      RelativeLayout relativeLayout = new RelativeLayout(this);
      RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.FILL_PARENT,
              RelativeLayout.LayoutParams.FILL_PARENT);

      // Creating a new TextView
      TextView tv = new TextView(this);
      tv.setText(msgdesc[i]);
      tv.setTop(20);        

      // Defining the layout parameters of the TextView
      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.WRAP_CONTENT,
              RelativeLayout.LayoutParams.WRAP_CONTENT);
      lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

      // Setting the parameters on the TextView
      tv.setLayoutParams(lp);

      // Adding the TextView to the RelativeLayout as a child
      bubbleLayout.addView(tv);

我的 XML 文件:

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

    <RelativeLayout
        android:layout_width="1300dip"
        android:layout_height="80dip"
        android:background="#084B8A" >

        <TextView
            android:id="@+id/messagetitle"
            android:layout_width="wrap_content"
            android:layout_height="90dip"
            android:layout_centerInParent="true"
            android:layout_marginTop="30dip"
            android:textColor="#ffffff"
            android:textSize="12pt" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layoutbubble"
        android:layout_width="1300dip"
        android:layout_height="500dip"
        android:layout_marginTop="10dip" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="1300dip"
        android:layout_height="80dip"
        android:layout_marginTop="10dip" >

        <EditText
            android:id="@+id/replytext"
            android:layout_width="1000dip"
            android:layout_height="90dip"
            android:layout_marginLeft="10dip" />

        <Button
            android:id="@+id/sendbutton"
            android:layout_width="200dip"
            android:layout_height="90dip"
            android:layout_marginLeft="1050dp"
            android:background="#084B8A"
            android:text="Send"
            android:textColor="#ffffff"
            android:textSize="12pt" />
    </RelativeLayout>

</LinearLayout>

截屏

4

2 回答 2

3

我解决了我的问题。在 xml 中RelativeLayout更改为LinearLayout

<LinearLayout
        android:id="@+id/layoutbubble"
        android:layout_width="1300dip"
        android:layout_height="500dip"
        android:layout_marginTop="10dip"
        android:orientation="vertical">
</LinearLayout>

并将我的代码更改如下:

bubbleLayout=(LinearLayout)findViewById(R.id.layoutbubble);
for(int i=0;i<msgid.length;i++){  
    // Creating a new TextView
    TextView tv = new TextView(this);
    tv.setText(msgdesc[i]);
    tv.setPadding(30, 10, 0, 0);         
    bubbleLayout.addView(tv);         
}

这就对了。

于 2013-01-21T09:20:17.273 回答
2

我认为您使用的是RelativeLayout 还是普通的LinearLayout?

尝试使用 LinearLayoutandroid:orientation="vertical"

于 2013-01-18T12:38:07.270 回答