【问题标题】:Can't change TextView when in RelativeLayout在 RelativeLayout 中时无法更改 TextView
【发布时间】: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


【解决方案1】:

因为有这么多的textview,你需要你的weight textview低于#001 textview。

你很幸运我知道口袋妖怪

tvPEntry -> 我猜这是您设置文本的文本“#001”

请在下方查看。我在tvPEntry下面添加了textview tvWeight

<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" />

<TextView
    android:id="@+id/tvWeight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvPEntry"
    android:layout_below="@+id/tvPName"
    android:layout_marginTop="14dp"
    android:text="Weight" />


<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" />


<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>

【讨论】:

  • 那么你的意思是,通过改变它在代码中的相对位置,它将分配到程序中的不同区域?我认为这是通过对齐参考确定的(例如 layout_alignTop="@id/ivType2")
  • 有了所有这些文本视图,它已经令人困惑了。我已经改变了入口文本视图下方的权重文本视图的位置。只有对齐解决不了。你也需要使用 layout_below 属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2020-10-20
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
相关资源
最近更新 更多