点击一个按钮,并在屏幕的一半上改变颜色
所以,我有1个屏幕被切成两部分。第一部分包含4个按钮(红色,蓝色,绿色,紫色),第二部分是空白区域。 我想要的是,当我按下按钮1时,我想要对应的颜色出现在空白处。点击一个按钮,并在屏幕的一半上改变颜色
如何创建和空空间
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<Button
android:layout_width="200sp"
android:layout_height="50sp"
android:id="@+id/button1"
android:text="Red"/>
<Button
android:layout_width="200sp"
android:layout_height="50sp"
android:id="@+id/button2"
android:layout_toRightOf="@+id/button1"
android:text="Blue"/>
<Button
android:layout_width="200sp"
android:layout_height="50sp"
android:id="@+id/button3"
android:layout_below="@+id/button1"
android:text="Green"/>
<Button
android:layout_width="200sp"
android:layout_height="50sp"
android:id="@+id/button4"
android:layout_toRightOf="@+id/button3"
android:layout_below="@+id/button2"
android:text="Purple"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:id="@+id/bg2"
android:background="@drawable/selector">
</RelativeLayout>
</LinearLayout>
在你的onCreate添加
Button button1 = (Button)findViewById(R.id.button1);
RelativeLayout rel =(RelativeLayout)findViewById(R.id.bg2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rel.setBackgroundColor(Color.RED);
}
});
即时通知bg2和Color.Red –
“符号无法解析”的错误噢,我的坏它是RED而不是红色。而bg2假设是你的xml文件中给出的id。 也许你必须导入导入android.widget.RelativeLayout;并导入android.graphics.Color; –
我已经导入了这两个项目,而“bg2”仍然不适合... –
你应该在这个活动创建两个片段,并创建一个米姆两个部分介绍这两个碎片按键之间的连接,所以这两个片段可以通讯 然后你可以发送和接收两个片段之间的数据
不需要2个片段可以在1个片段中使用布局完成 –
看看这个问题:
你基本上遵循相同的步骤,除了你调用相应的setBackgroundColor(Color.parseColor( “XYZ”)); 作为按钮的onClick()被调用时按钮的颜色。
你做了一些Java类来做到这一点? –