我有两个水平并排放置的TextView,它们是带圆角的矩形。
我想:
两个TextView的Xml代码:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:background="@drawable/toggle"
android:id="@+id/l1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coach"
android:paddingLeft="5dp"
android:textColor="#ffffff"
android:id="@+id/TV"
android:layout_gravity="center_horizontal"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Client"
android:paddingRight="5dp"
android:textColor="#ffffff"
android:id="@+id/TV1"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
_____ Java Code ________
package com.example.jhang.sample;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import static android.R.color.black;
import static android.R.color.white;
/**
* Created by jhang on 5/2/2016.
*/
public class Activity1 extends Activity implements View.OnClickListener {
TextView coach, client, about, privacy, faq, contact,login;
LinearLayout l1;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
coach=(TextView)findViewById(R.id.TV);
client=(TextView)findViewById(R.id.TV1);
about=(TextView)findViewById(R.id.TV2);
privacy=(TextView)findViewById(R.id.TV3);
faq=(TextView)findViewById(R.id.TV4);
contact=(TextView)findViewById(R.id.TV5);
login=(TextView)findViewById(R.id.button);
l1=(LinearLayout)findViewById(R.id.l1);
coach.setOnClickListener(this);
client.setOnClickListener(this);
about.setOnClickListener(this);
privacy.setOnClickListener(this);
faq.setOnClickListener(this);
contact.setOnClickListener(this);
login.setOnClickListener(this);
l1.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (count==0)
{
coach.setBackgroundColor(Color.WHITE);
coach.setTextColor(Color.BLACK);
count=1;
}
else if (count==1)
{
client.setBackgroundColor(Color.WHITE);
client.setTextColor(Color.BLACK);
count=0;
}
}
}
答案 0 :(得分:1)
public void onClick(View v)
{
if (count==0)
{
client.setBackgroundColor(Color.BLACK);
client.setTextColor(Color.WHITE);
coach.setBackgroundColor(Color.WHITE);
coach.setTextColor(Color.BLACK);
count=1;
}
else if (count==1)
{
coach.setBackgroundColor(Color.BLACK);
coach.setTextColor(Color.White);
client.setBackgroundColor(Color.WHITE);
client.setTextColor(Color.BLACK);
count=0;
}
}
答案 1 :(得分:0)
尝试这种方式,创建这个xml button_press
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/blue" />
<stroke
android:width="4dp"
android:color="@color/blue" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_press"
android:onClick="onClick"
android:text="Press 1" />
<TextView
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_press"
android:onClick="onClick"
android:text="Press 2" />
</LinearLayout>
爪哇
Textview button;
public void onClick(View v) {
Drawable dr = getResources().getDrawable(R.drawable.button_press);
dr.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP);
switch (v.getId()) {
case R.id.btn:
if (button == null) {
button = (TextView ) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_press);
button = (TextView ) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
break;
case R.id.btn2:
if (button == null) {
button = (TextView ) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_press);
button = (TextView ) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
break;
default:
break;
}
}