java 约束布局背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45348491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ConstraintLayout background
提问by Frekzok
I was trying to change background by clicking a button, but I can't identify Constraint Layout in java code.
我试图通过单击按钮来更改背景,但我无法在 java 代码中识别约束布局。
activity_main.xml:
活动_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
tools:context="frekzok.trafficlight.MainActivity">
//here are some button options
</android.support.constraint.ConstraintLayout>
MainActivity.java:
主活动.java:
package frekzok.trafficlight;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private ConstraintLayout mConstraintLayout;
private TextView mInfoTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout =
(ConstraintLayout)findViewById(R.id.constraintLayout);
mInfoTextView = (TextView)findViewById(R.id.textView2);}
public void onRedButtonClick(View view) {
mInfoTextView.setText(R.string.red);
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
}
public void onYellowButtonClick(View view) {
mInfoTextView.setText(R.string.yellow);
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
}
public void onGreenButtonClick(View view) {
mInfoTextView.setText(R.string.green);
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
}
}
In Main Activity, on line 18, there is a error on "constraintLayout": Cannot resolve symbol 'constraintLayout' How can I change background color?
在 Main Activity 的第 18 行,“constraintLayout”出现错误:无法解析符号 'constraintLayout' 如何更改背景颜色?
采纳答案by Amaury Medeiros
You didn't add the ID to your constraint layout. Try modifying your xml file to include it:
您没有将 ID 添加到约束布局中。尝试修改您的 xml 文件以包含它:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
tools:context="frekzok.trafficlight.MainActivity">
</android.support.constraint.ConstraintLayout>