gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:14:35 24 4
gpt4 key购买 nike

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

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

然后,我在谷歌上搜索了一些样本来改善我的 table 的外观,我发现了这个:

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. 所说,我必须更改表构造函数的参数

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

enter image description here

最佳答案

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

然后使用以下内容:

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

编辑关于您的编辑

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

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

关于java - 如何以编程方式将 LinearLayout 对象添加到我的上下文中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com