我对android开发很新,所以如果我不明白到底发生了什么,我很抱歉。
我已经创建了一个形状,如下所示。它包含在drawable文件夹中的shape.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="@color/lboro_pink"/>
<stroke
android:width="50dp"
android:color="@color/lboro_blue"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
现在在我的activity_main.xml中我有这个TextView:
<TextView
android:id="@+id/grid_1"
android:background="@drawable/shape"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="@string/event_1"/>
现在这是我的MainActivity.java:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.res.Resources;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.test_image);
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.shape);
TextView tv = (TextView)findViewByID(R.id.grid_1);
tv.setBackground(shape);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
为了做到这一点,我一直在尝试关注this doc,并且通过使用他们的代码我只是得到了“无法解析方法findViewByID”的错误。我已经搜索了答案,但是当官方的android文档说这样做时,他们无法理解他们想要解释的内容。
我想要做的就是将我创建的形状应用为TextView的背景。
非常感谢任何帮助。谢谢:))
答案 0 :(得分:5)
TextView tv = (TextView)findViewByID(R.id.grid_1);
此行拼写错误。它应该是
TextView tv = (TextView)findViewById(R.id.grid_1);