在相对布局中,如何在另一个textview下面显示运行时textview?

时间:2015-06-08 12:32:55

标签: java android

我想显示每个国家/地区的提示。当我单击国家/地区名称时,它应显示说明。为此,我设计了XML,其相对布局包含两个或多个文本视图(country1,country2)和一个文本视图,以便在单击任何国家/地区时显示描述。

问题是,当我点击国家1时,国家2不会显示运行时文本视图。

但是对于国家3,当我点击它时,它显示在底部。所以这意味着文本视图没有对齐。我得到了这个概念,但我仍然坚持如何实现它。

我正在使用API​​版本21

这是我的Java:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class savev extends Activity implements OnClickListener{
    TextView t1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.country);
        t1=(TextView) findViewById(R.id.textview4);
        t1.setVisibility(View.GONE);
        findViewById(R.id.textview1).setOnClickListener(this);
        findViewById(R.id.textview2).setOnClickListener(this);
        findViewById(R.id.textview3).setOnClickListener(this);
    }


    public void toggle_contents(View v,int r,int s){

        /*RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        p.addRule(RelativeLayout.BELOW, r);*/
        t1.setVisibility(t1.isShown()? View.GONE: View.VISIBLE);
        t1.setText(s);
        /*t1.setLayoutParams(p);*/
    }

     @Override
     public void onClick(View v) 
     {
          switch (v.getId())
          {
          case R.id.textview1:
              toggle_contents(v,R.id.textview1,R.string.country1Meaning);
          break;
          case R.id.textview2:
              toggle_contents(v,R.id.textview2,R.string.country2Meaning);
          break;
          case R.id.textview3:
              toggle_contents(v,R.id.textview3,R.string.country1Meaning);
          break;

          default:break;
          }
     }
}

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:fillViewport="true">
    <RelativeLayout
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rl"
        >
        <TextView
                android:id="@+id/textview1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:color="#FFFFFF" 
                android:padding="25dip"
                android:text="country1"
                android:textStyle="bold"
                android:clickable="true"
                android:background="#D8D8D8"
                >
        </TextView>

        <TextView
                android:id="@+id/textview4"
                android:drawablePadding="12dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="12dip"
                android:drawableLeft="@drawable/customborder"
                android:drawableStart="@drawable/customborder"
                android:background="#FF0000"
                >
        </TextView>

        <TextView
                android:id="@+id/textview2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:color="#FFFFFF" 
                android:padding="25dip"
                android:text="country2"
                android:textStyle="bold"
                android:clickable="true"
                android:background="#D8D8D8"    
                android:layout_below="@+id/textview1"   
                >
        </TextView>
         <TextView
                android:id="@+id/textview3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:color="#FFFFFF" 
                android:padding="25dip"
                android:text="country3"
                android:textStyle="bold"
                android:clickable="true"
                android:background="#D8D8D8"
                android:layout_below="@+id/textview2"       
                >
        </TextView>


    </RelativeLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

将相对布局更改为线性

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:fillViewport="true">
    <LinearLayout
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mainLayout"
        >
        <TextView
                android:id="@+id/textview1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:color="#FFFFFF" 
                android:padding="25dip"
                android:text="country1"
                android:textStyle="bold"
                android:clickable="true"
                android:background="#D8D8D8"
                >
        </TextView>

        <TextView
                android:id="@+id/textview2"
                android:drawablePadding="12dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="12dip"
                android:drawableLeft="@drawable/customborder"
                android:drawableStart="@drawable/customborder"
                android:background="#FF0000"
                >
        </TextView>

        <TextView
                android:id="@+id/textview3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:color="#FFFFFF" 
                android:padding="25dip"
                android:text="country2"
                android:textStyle="bold"
                android:clickable="true"
                android:background="#D8D8D8"    
                android:layout_below="@+id/textview1"   
                >
        </TextView>
         <TextView
                android:id="@+id/txtDescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:color="#FFFFFF" 
                android:padding="25dip"
                android:visibility="gone
                android:textStyle="bold"
                android:clickable="true"
                android:background="#D8D8D8"
                android:layout_below="@+id/textview2"       
                >
        </TextView>


    </RelativeLayout>
</ScrollView>

动态更改说明文字视图位置:

    public class savev extends Activity implements OnClickListener{
        TextView txtDesc;
ViewGroup mainContainer; //Linearlayout with textviews
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.country);
            txtDesc=(TextView) findViewById(R.id.textview4);
            mainContainer = (ViewGroup) findViewId(R.id.mainLayout);
            txtDesc.setVisibility(View.GONE);
            findViewById(R.id.textview1).setOnClickListener(this);
            findViewById(R.id.textview2).setOnClickListener(this);
            findViewById(R.id.textview3).setOnClickListener(this);
        }




         @Override
         public void onClick(View v) 
         {
              switch (v.getId())
              {
              case R.id.textview1:
                 txtDesc.setText(getString(R.string.country1Meaning));                  
              break;
              case R.id.textview2:
                  txtDesc.setText(getString(R.string.country2Meaning)); 
              break;
              case R.id.textview3:
                  txtDesc.setText(getString(R.string.country3Meaning)); 
              break;

              default:break;
              }
              mainContainer.removeView (txtDesc)
              int pos = mainContainer.indexOfChild(v);
              if(pos>0) {
                   txtDesc.setVisibility(VISIBLE);
                   if(pos == getChildCount)
                   mainContainer.addView(txtDesc);
                   else 
                    mainContainer.addView(txtDesc,pos+1);
              }

         }
    }