大概步骤建立几个Activity并将图片分别关联到其中,并进行布局操作,一个是主要的界面,另一个是进入Shop后的界面,操作的具体代码和结果如下:
代码:
activity_char.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- />
- <ImageView
- android:id="@+id/iv"
- android:layout_width="120dp"
- android:layout_height="150dp"
- android:layout_gravity="center"
- android:layout_marginTop="50dp"
- android:background="@drawable/soldier76"/>
- <TextView
- android:id="@+id/CN"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginTop="5dp"
- android:textSize="23sp"
- android:text="Soldier76"
- />
- <TextView
- android:id="@+id/status"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginTop="5dp"
- android:layout_marginBottom="3dp"
- android:text="Wonderful"
- android:textColor="#7CFC00"/>
- <TableLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center">
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Health"
- android:textSize="20sp"
- android:layout_weight="1"
- />
- <ProgressBar
- android:id="@+id/pro_health"
- style="@style/Widget.AppCompat.ProgressBar.Horizontal"
- android:layout_weight="2"
- android:indeterminateTint="#7CFC00"
- android:indeterminateTintMode="src_atop"/>
- <TextView
- android:id="@+id/status_hea"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="3"
- />
- </TableRow>
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Power"
- android:textSize="20sp"
- android:layout_weight="1"
- />
- <ProgressBar
- android:id="@+id/pro_power"
- style="@style/Widget.AppCompat.ProgressBar.Horizontal"
- android:layout_weight="2"
- android:indeterminateTint="#6495ED"
- android:indeterminateTintMode="src_atop"/>
- <TextView
- android:id="@+id/status_pow"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="3"
- />
- </TableRow>
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Sensitivity"
- android:textSize="20sp"
- android:layout_weight="1"
- />
- <ProgressBar
- android:id="@+id/pro_sen"
- style="@style/Widget.AppCompat.ProgressBar.Horizontal"
- android:layout_weight="2"
- android:indeterminateTint="#EEEE00"
- android:indeterminateTintMode="src_atop"/>
- <TextView
- android:id="@+id/status_sen"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="3"
- />
- </TableRow>
- </TableLayout>
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginTop="20dp"
- android:text="Go to Shop"/>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- android:id="@+id/rr"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <TextView
- android:id="@+id/item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Item"
- android:layout_marginLeft="60dp"
- android:layout_marginTop="20dp"
- android:textSize="20sp"
- android:layout_centerVertical="true"
- />
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@+id/item"
- android:layout_marginLeft="10dp"
- android:layout_centerInParent="true"
- android:orientation="vertical">
- <TextView
- android:id="@+id/health"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Health"
- android:textSize="23sp"/>
- <TextView
- android:id="@+id/power"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Power"
- android:textSize="23sp"/>
- <TextView
- android:id="@+id/sen"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Sen"
- android:textSize="23sp"/>
- </LinearLayout>
- </RelativeLayout>
ShopAct.java:
- package com.eee.eeeee;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.TextView;
- import cn.itcast.domain.ItemInfo;
- /**
- * Created by li124 on 2017/2/25.
- */
- public class ShopAct extends AppCompatActivity implements View.OnClickListener{
- private ItemInfo itemInfo;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_shop);
- itemInfo =new ItemInfo("Tactical eyepiece",80,100,60);
- findViewById(R.id.rr).setOnClickListener(this);
- TextView h=(TextView)findViewById(R.id.health);
- TextView p=(TextView)findViewById(R.id.power);
- TextView s=(TextView)findViewById(R.id.sen);
- TextView n=(TextView)findViewById(R.id.item);
- n.setText(itemInfo.getName()+"");
- h.setText("Health++"+itemInfo.getHealth());
- p.setText("Power++"+itemInfo.getPower());
- s.setText("Sen++"+itemInfo.getSen());
- }
- @Override
- public void onClick(View v){
- switch (v.getId()){
- case R.id.rr:
- Intent intent =new Intent();
- intent.putExtra("equip",itemInfo);
- setResult(1,intent);
- finish();
- break;
- }
- }
- }