适用于Android的Java:扩展XML布局,TextView的重叠

时间:2011-05-27 23:33:23

标签: java android android-layout

所以这是我的交易。我有一个连接到PHP站点的应用程序,它获取JSON数据并解析出来。 JSON解析非常有效,因此无需深入研究。我需要从PHP站点允许无限量的项目,所以我正在创建一个循环来为每个项目膨胀XML文件。我的问题是TextView项lblShared和lblSaid重叠lblQuote。他们假设在lblQuote下方对齐。我会发布我的源代码的基础知识,如果需要请求更多。

LinearLayout l = (LinearLayout) findViewById(R.id.myMainLayout);
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View myView = linflater.inflate(R.layout.myviews, null);
TextView tvQuote = (TextView) myView.findViewById(R.id.lblQuote);
//Set an id to know which quote is in use
tvQuote.setId(i);
TextView tvShared = (TextView) myView.findViewById(R.id.lblShared);
TextView tvSaid = (TextView) myView.findViewById(R.id.lblSaid);

//Change name dynamically
tvQuote.setText((strQuote).toString());
tvShared.setText(("Shared by: " + strShared).toString());
tvSaid.setText(("Said by: " + strSaid).toString());
l.addView(myView);

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        android:orientation="vertical" 
        android:id="@+id/myMainLayout" 
        android:layout_width="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_height="wrap_content">
    <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:text="Title" 
        android:id="@+id/lblTitle" 
        android:textSize="16px" 
        android:padding="5px" 
        android:textStyle="bold" 
        android:gravity="center_horizontal">
    </TextView>
</LinearLayout>

myviews.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip">

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

    <TextView 
        android:id="@+id/lblShared" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_below="@id/lblQuote"
        android:layout_alignParentRight="true"/>
    <TextView 
        android:id="@+id/lblSaid" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_below="@id/lblQuote"
        android:layout_alignParentLeft="true"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果要将它们堆叠起来,为什么不使用另一个LinearLayout??

相对并不意味着通过设计将元素叠加在一起。