【发布时间】:2013-12-31 07:52:49
【问题描述】:
我有一个文本视图,需要在我的活动中放置到另一个相对布局中。 但是,当我这样做时,分配给它的文本 (pokeDesc) 出于某种原因更改为 (pokeWeight)。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivPokeView"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="18dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tvPName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ivPokeView"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/ivPokeView"
android:text="@string/pName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvPEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvPName"
android:layout_below="@+id/tvPName"
android:layout_marginTop="14dp"
android:text="@string/pEntry" />
<ImageView
android:id="@+id/ivType1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ivPokeView"
android:layout_marginRight="16dp"
android:layout_marginTop="15dp"
android:layout_toLeftOf="@+id/ivType2"
android:src="@drawable/fire" />
<ImageView
android:id="@+id/ivType2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/ivPokeView"
android:layout_alignTop="@+id/ivType1"
android:layout_marginRight="23dp"
android:src="@drawable/grass" />
<TextView
android:id="@+id/tvHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvPEntry"
android:layout_below="@+id/tvPEntry"
android:layout_marginTop="17dp"
android:text="Height" />
<TextView
android:id="@+id/tvWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvHeight"
android:layout_alignBottom="@+id/tvHeight"
android:layout_toRightOf="@+id/tvPName"
android:text="Weight" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ivType1"
android:layout_margin="20dip"
android:layout_marginTop="19dp"
android:background="@drawable/rect_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/tvDesc"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="5dp"
android:layout_marginLeft="2dp"
android:text="@string/pDesc"
android:textSize="17dp" />
</RelativeLayout>
</RelativeLayout>
分配文本视图变量的代码
//Initalises all UI items in the activity
public void initVars(){
pName = (TextView) findViewById(R.id.tvPName);
pEntry = (TextView) findViewById(R.id.tvPEntry);
pDesc = (TextView) findViewById(R.id.tvDesc);
pHeight = (TextView) findViewById(R.id.tvHeight);
pWeight = (TextView) findViewById(R.id.tvWeight);
pPic = (ImageView) findViewById(R.id.ivPokeView);
tPic1 = (ImageView) findViewById(R.id.ivType1);
tPic2 = (ImageView) findViewById(R.id.ivType2);
}
从 JSON 文件中获取数据的代码
//Sets the pokemons name
pokeName = pindex.getString("Name");
pName.setText(pokeName);
//Sets the pokemons entry
pokeEntry = pindex.getString("Entry");
pEntry.setText(pokeEntry);
//Sets pokemon description
pokeDesc = pindex.getString("desc");
pDesc.setText(pokeDesc);
//Sets height
pokeHeight = pindex.getString("height");
pHeight.setText("Height: "+pokeHeight);
//Sets weight
pokeWeight = pindex.getString("weight");
pWeight.setText("Weigth: "+pokeWeight);
【问题讨论】:
-
你有这么多的文本视图。你说的是哪一个?
标签: android xml layout textview