问题描述
我试图使用Android注解和动态添加布局组件我单击 createNewRow 按钮时。该应用程序运行并显示在定义的默认行 activity_main.xml中并,点击后的 createNewButton 我看到在调试器连接到我的 dynamicTable 的孩子,但不显示新的儿童。这是我的主要活动XML:
I'm trying to use Android Annotations and dynamically add layout components when my createNewRow button is clicked. The app runs and displays the default rows defined in activity_main.xml and, after clicking createNewButton I see children attached to my dynamicTable in the debugger but the new children are not displayed. Here is my main activity XML:
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/inflateLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="net.richardriley.inflate.app.MainActivity">
<Button
android:id="@+id/createNewRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/newRowButton"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dynamicTable">
<TableRow>
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
所以,简单地用一个按钮,然后一个表容器的线性布局。单击 createNewRow 应该添加一个新的 InflatedRow 。下面是我要动态地添加该行的XML:
So simply a linear layout with a button and then a table container. Clicking createNewRow should add a new InflatedRow. Here is the XML for the row I want to dynamically add:
inflatedrow.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="@+id/inflatedRowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inflatedRowLabel"/>
<Button
android:id="@+id/inflatedRowButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/defaultNewRowText"
/>
</merge>
使用AA我对通过tablerow子类
Using AA my subclass for a tableRow is
InflatedRow.java
package net.richardriley.inflate.app;
import android.content.Context;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TextView;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
/**
* inflate : Created by rgr on 20/03/14.
*/
@EViewGroup(R.layout.inflatedrow)
public class InflatedRow extends TableRow {
@ViewById
Button inflatedRowButton;
@ViewById
TextView inflatedRowTextView;
public InflatedRow(Context context) {
super(context);
}
}
终于我的主要活动Java本身:
and finally my main activity java itself:
MainActivity.java
package net.richardriley.inflate.app;
import android.support.v7.app.ActionBarActivity;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import org.androidannotations.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static ch.qos.logback.classic.android.BasicLogcatConfigurator.configureDefaultContext;
@OptionsMenu(R.menu.main)
@EActivity(R.layout.activity_main)
public class MainActivity extends ActionBarActivity {
static {
configureDefaultContext();
log = LoggerFactory.getLogger(MainActivity.class);
}
@ViewById
LinearLayout inflateLayout;
@ViewById
TableLayout dynamicTable;
public MainActivity() {
}
protected static Logger log;
@Click
void createNewRow() {
log.info("clicked");
InflatedRow_ inflatedRow=new InflatedRow_(this);
dynamicTable.addView(inflatedRow,0);
/*LayoutInflater layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.inflatedrow,null);
dynamicTable.addView(view);*/
}
@OptionsItem
void openSettingsSelected(){
log.info("Hello");
}
}
在 createNewRow 如果我直接用充气服务,它的工作原理。
In createNewRow if I use the inflater service directly it works.
我是什么失踪?
非常感谢。
推荐答案
当你膨胀的观点,不要使用注解的类,并确保你调用了它膨胀的构建方法。
Don't use the annotated class when you're inflating the view and make sure you're calling the build method that inflates it.
@Click
void createNewRow() {
log.info("clicked");
InflatedRow inflatedRow = new InflatedRow_.build(this);
dynamicTable.addView(inflatedRow,0);
}
这篇关于是否可以动态地添加AndroidAnnotation @EViewGroups,而不是直接采用传统的充气?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!