单击按钮并更改屏幕一半的颜色

时间:2016-03-03 15:14:41

标签: javascript android button colors

所以,我有1个屏幕分为2部分。第一部分包含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>

3 个答案:

答案 0 :(得分:0)

你应该在这个活动中创建两个片段,并创建一个meme两个关联这两个片段,所以这两个片段可以通信 然后你可以在两个片段之间发送和接收数据

答案 1 :(得分:0)

看看这个问题:

set background color: Android

除了调用相应的setBackgroundColor(Color.parseColor(&#34; xyz&#34;))之外,你基本上都遵循相同的步骤;

。调用按钮的onClick()按钮的颜色。

答案 2 :(得分:0)

在你的onCreate add

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);           
           }
        });
相关问题