在图像视图中显示文本视图

时间:2017-02-07 08:16:56

标签: android

有人可以帮帮我吗?我无法在图片视图中的textview上显示结果,有人可以帮我看看。我想在图像视图中的textview上显示结果。我试图改变很多次并重新看一下,但是我仍然无法找到任何出错的地方。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="sg.edu.rp.c346.lovecalculator.MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/b_calculate"
    android:layout_centerHorizontal="true"
    android:scaleType="centerInside"
    android:src="@drawable/heart" />

<EditText
    android:id="@+id/et_female"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Female name" />

<EditText
    android:id="@+id/et_male"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et_female"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Male name" />

<Button
    android:id="@+id/b_calculate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et_male"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="Calculate" />

<TextView
    android:id="@+id/tv_result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignTop="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text=""
    android:textColor="#ef3737"
    android:textSize="32dp" />

public class MainActivity extends AppCompatActivity {

EditText et_female, et_male;
Button b_calculate;
TextView tv_result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et_female = (EditText) findViewById(R.id.et_female);
    et_male = (EditText) findViewById(R.id.et_male);
    b_calculate = (Button) findViewById(R.id.b_calculate);
    tv_result = (TextView) findViewById(R.id.tv_result);

    b_calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (et_female.getText().toString().equals("")
                    && et_male.getText().toString().equals("")) {

                String female = et_female.getText().toString();
                String male = et_male.getText().toString();

                Calendar c = Calendar.getInstance();
                String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
                String month = String.valueOf(c.get(Calendar.MONTH));
                String year = String.valueOf(c.get(Calendar.YEAR));

                String result_string = female + male + day + month + year;
                result_string = result_string.toLowerCase();
                result_string = result_string.trim();

                int seed = result_string.hashCode();

                Random r = new Random(seed);

                tv_result.setText((r.nextInt(100) + 1) + "%");

            }

        }
    });


}

}

2 个答案:

答案 0 :(得分:0)

您可以设置textview的背景。

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">


    <EditText
        android:id="@+id/et_female"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:hint="Female name" />

    <EditText
        android:id="@+id/et_male"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_female"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:hint="Male name" />

    <Button
        android:id="@+id/b_calculate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_male"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Calculate" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b_calculate"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:background="@drawable/heart"
        android:text="510"
        android:padding="10dp"
        android:textColor="#ef3737"
        android:textSize="32dp" />
</RelativeLayout>

答案 1 :(得分:0)

使用如下框架布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <ImageView 
      android:src="@drawable/ic_launcher"
      android:scaleType="fitCenter"
      android:layout_height="250px"
      android:layout_width="250px"/>

   <TextView
      android:text="Frame Demo"
      android:textSize="30px"
      android:textStyle="bold"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:gravity="center"/>
</FrameLayout>

相应地更改大小,框架布局会自动将第二个孩子放在第一个孩子上方,因此textview将位于imageview上方而不会隐藏在它后面。

享受。

相关问题