[英]Java for Android: Inflating XML Layouts, TextView's overlapping
所以這是我的交易。 我有一個連接到 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>
如果您要像那樣堆疊它們,為什么不直接使用另一個 LinearLayout..?
相對並不意味着通過設計將元素堆疊在彼此的頂部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.