如何以编程方式将LinearLayout对象添加到我的上下文中? [英] How to programatically add LinearLayout object to my context?

查看:48
本文介绍了如何以编程方式将LinearLayout对象添加到我的上下文中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序,它具有以下特征:

I have my app, it has the following appareance:

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

The table at the bottom side is a TableLayout. In my MainActivity.java I am using the following code to populate the whole layout of my app:

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);

然后,我在Google上搜索了一些示例以改善桌子的外观,结果发现:

Then, I googled for some samples to improve the appareance of my table, and I found this:

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

But this sample builds only the table. In the MainActivity.java the code used for building it :

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

和表类:

public class Table extends LinearLayout {
..
}

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

Now, what I want is to add the smarter table to the original layout of my app.

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

I added an empty LinearLayout component in my activity_main.xml and I tried to populate it with this code:

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);

但是它不起作用. 抱歉,这是一个非常基本的问题,但是,您能告诉我哪些是将智能表添加到我的应用的原始布局中的正确语句吗?

But it does not work. Sorry for the very basic question, but, can you tell me which are the right statements for adding the smarter table to the original layout of my app?

编辑2017年11月27日

遵循关于Mike M.和F43nd1r的建议.

Following the suggestion on Mike M. and F43nd1r.

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

I configured my activity_main.xml in this way:

<?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中使用了以下代码

And I used the following code in my 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构造函数的参数

as Mike M. says, I had to change the parameters of the Table constructor

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

推荐答案

在xml布局中使用<com.your.package.Table>代替<LinearLayout>.

Instead of <LinearLayout> use <com.your.package.Table> in the xml layout.

然后使用以下内容:

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


根据您的修改进行编辑

将此构造函数添加到表中:


Edit regarding your edit

Add this constructor to Table:

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

这篇关于如何以编程方式将LinearLayout对象添加到我的上下文中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆