如何以编程方式将LinearLayout对象添加到我的上下文中?

时间:2017-11-26 23:00:47

标签: java android android-layout

我有我的应用程序,它有以下外观:

enter image description here

底部的表是TableLayout。 在我的MainActivity.java中,我使用以下代码填充我的应用程序的整个布局:

setContentView(R.layout.activity_main);  

movieCompletionView = (TokenCompletionView)findViewById(R.id.searchMovie);
cineCompletionView = (TokenCompletionView)findViewById(R.id.searchCine);
...          
TableLayout tl_Movies = (TableLayout)findViewById(R.id.tableLayoutMovies);

tl_Movies.removeAllViews();

for (...) tl_Movies.addView(someRow);

然后,我用谷歌搜索了一些样品来改善桌子的外观,我发现了这个:

enter image description here

但是此示例仅构建表。在MainActivity.java中,用于构建它的代码:

Table table = new Table(this);
setContentView(table);

和表类:

public class Table extends LinearLayout {
..
}

现在,我想要的是将更智能的表格添加到我的应用程序的原始布局中。

我在activity_main.xml中添加了一个空的LinearLayout组件,我尝试使用以下代码填充它:

setContentView(R.layout.activity_main);

movieCompletionView = (TokenCompletionView)findViewById(R.id.searchMovie);
cineCompletionView = (TokenCompletionView)findViewById(R.id.searchCine);
...

linearLayout = (LinearLayout) findViewById(R.id.smarterTable);
linearLayout = new Table(this);

但它不起作用。 很抱歉这个非常基本的问题,但是,您能否告诉我哪些是将更智能的表格添加到我的应用程序原始布局的正确陈述?

编辑2017年11月27日

遵循Mike M.和F43nd1r的建议。

我以这种方式配置了我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    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="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.myapps.pavel.myseapp.MainActivity">

    <com.myapps.pavel.myseapp.Table
        android:id="@+id/smarterTable"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"/>

</LinearLayout>

我在MainActivity.java中使用了以下代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    linearLayout = (Table) findViewById(R.id.smarterTable);

我收到了这个错误:

FATAL EXCEPTION: main
Process: com.myapps.pavel.myseapp, PID: 5875
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapps.pavel.myseapp/com.myapps.pavel.myseapp.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.myapps.pavel.myseapp.Table
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.myapps.pavel.myseapp.Table
 Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.myapps.pavel.myseapp.Table
 Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
    at java.lang.Class.getConstructor0(Class.java:2204)
    at java.lang.Class.getConstructor(Class.java:1683)
    at android.view.LayoutInflater.createView(LayoutInflater.java:618)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.myapps.pavel.myseapp.MainActivity.onCreate(MainActivity.java:83) /* This lane has this statement: setContentView(R.layout.activity_main);*/
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

有什么建议吗? 感谢

解决

正如Mike M.所说,我必须更改Table构造函数的参数

public Table(Context context,AttributeSet attrs) {
    super(context, attrs);

enter image description here

1 个答案:

答案 0 :(得分:1)

而不是<LinearLayout>在xml布局中使用<com.your.package.Table>

然后使用以下内容:

linearLayout = (Table) findViewById(R.id.smarterTable);
for (...) linearLayout.addView(someRow);

编辑您的编辑

将此构造函数添加到表:

public Table(Context context, AttributeSet attrs) {
    super(context, attrs);
}