android mainactivitystudio的main_activity.xml添加view时控件报错

Android在布局中动态添加view的两种方法
添加视图文件的时候有两种方式:1、通过在xml文件定义layout;2、java代码编写
二、前言说明
1.构造xml文件
2.LayoutInflater
提到addview,首先要了解一下LayoutInflater类。这个类最主要的功能就是实现将xml表述的layout转化为View的功能。为了便于理解,我们可以将它与findViewById()作一比较,二者都是实例化某一对象,不同的是findViewById()是找xml布局文件下的具体widget控件实例化,而LayoutInflater找res/layout/下的xml布局文件来实例化的。
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);或
LayoutInflater inflater = LayoutInflater.from(Activity.this);或
LayoutInflater inflater = getLayoutInflater();
这三种方法本质是相同的。
(2)inflate()
用LayoutInflater.inflate() 将LayOut文件转化成VIew。
View view = inflater.inflate(R.layout.block_gym_album_list_item, null);
3.添加视图文件
1、通过在xml文件定义layout(block_gym_album_list_item.xml)
2、main.xml
3、DynamicViewActivity.xml
package com.gxtag.gym.
import android.app.A
import android.content.C
import android.os.B
import android.view.LayoutI
import android.view.V
import android.view.View.OnClickL
import android.view.ViewG
import android.widget.LinearL
import android.widget.LinearLayout.LayoutP
import android.widget.TextV
import com.gxtag.gym.R;
import com.icq.app.widget.StatedB
public class DynamicViewActivity extends Activity implements OnClickListener{
private Context mC
private TextView mTv_
private String title = "动态添加布局";
private StatedButton mSbtn_
private LinearLayout mLl_
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
initView();
mLl_parent.addView(addView1());
mLl_parent.addView(addView2());
private void initView() {
// TODO 初始化视图
mLl_parent=(LinearLayout) findViewById(R.id.ll_parent);
mTv_title = (TextView) findViewById(R.id.tv_title);
mTv_title.setText(String.format(String.format(
getResources().getString(R.string.title), title)));
mSbtn_back = (StatedButton) findViewById(R.id.sbtn_navback);
mSbtn_back.setOnClickListener(this);
private View addView1() {
// TODO 动态添加布局(xml方式)
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater1=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater2 = getLayoutInflater();
LayoutInflater inflater3 = LayoutInflater.from(mContext);
View view = inflater3.inflate(R.layout.block_gym_album_list_item, null);
view.setLayoutParams(lp);
private View addView2() {
// TODO 动态添加布局(java方式)
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout view = new LinearLayout(this);
view.setLayoutParams(lp);//设置布局参数
view.setOrientation(LinearLayout.HORIZONTAL);// 设置子View的Linearlayout// 为垂直方向布局
//定义子View中两个元素的布局
ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup.LayoutParams vlp2 = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setLayoutParams(vlp);//设置TextView的布局
tv2.setLayoutParams(vlp2);
tv1.setText("姓名:");
tv2.setText("李四");
tv2.setPadding(calculateDpToPx(50), 0, 0, 0);//设置边距
view.addView(tv1);//将TextView 添加到子View 中
view.addView(tv2);//将TextView 添加到子View 中
private int calculateDpToPx(int padding_in_dp){
final float scale = getResources().getDisplayMetrics().
(int) (padding_in_dp * scale + 0.5f);
public void onClick(View v) {
// TODO 控件单击事件
switch (v.getId()) {
case R.id.sbtn_navback:
this.finish();
4、有需要再联系我:qq-
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'您现在正在浏览:
SimpleLayoutGameActivity的View控件报错安卓android.widget.EditText cannot be cast to org.a
发布时间:
10:24:39 &
浏览次数:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:orientation="vertical" >
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="58dp"
android:ems="10"
android:gravity="left"
android:hint="@string/hello_world"
android:singleLine="false"
android:textSize="12sp" >
这个是manifist文件:
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="16" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
回答1:各位亲
我搞出来了
很低级的错误啊。。protected int getRenderSurfaceViewID() {
return R.id.editText1;这个id弄错了。。。哎
,希望大家多注意啊。。。
转载请保留出处:/mobile_development_question/155.html
本周热门问答排行关于网友提出的“activity_mainxml在R文件中没有自动生成”问题疑问,本网通过在网上对“activity_mainxml在R文件中没有自动生成”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
我用的android4.2版本,创建了一个project,但是在Java文件中通过R访问时却报错:
然后这个是我的目录结构:解决方案1:
project-&clean
以上介绍了“activity_mainxml在R文件中没有自动生成”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:/itwd/720597.html
上一篇: 下一篇:

我要回帖

更多关于 mainactivity 的文章

 

随机推荐