【问题标题】:How do I add a RelativeLayout (from XML) to a LinearLayout that I define programmatically?如何将 RelativeLayout(来自 XML)添加到我以编程方式定义的 LinearLayout?
【发布时间】:2012-06-28 23:32:10
【问题描述】:

我查看了其他问题,但没有发现任何与这种情况完全匹配的内容。我在 XML 中定义了一个 RelativeLayout。我想将整个 RelativeLayout 放在 GraphView 对象(来自我单独制作的类)下面。我认为将 RelativeLayout 放在 GraphView 下方的最佳方法是将它们都粘贴在我尝试以编程方式定义的 LinearLayout 中。

这是我目前所拥有的 - 有问题,但我不太确定是什么。

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class Grapher extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        LinearLayout ll = new LinearLayout(this); 
        GraphView gv = new GraphView(this); 
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainRelativeLayout);
        ll.addView(gv); 
        ll.addView(rl); 
        setContentView(ll); 

还有 xml....

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mainRelativeLayout"
    android:orientation="vertical">
    <Button
        android:id="@+id/second"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:text="@string/second" 
        android:textColor="@color/white"
        android:textSize="15sp"/>
    <Button
        android:id="@+id/alpha"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_below="@id/second"
        android:layout_alignParentLeft="true"
        android:text="@string/alpha"
        android:textColor="@color/white"
        android:textSize="15sp" />
    <Button
        android:id="@+id/mode"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/second"
        android:text="@string/mode"
        android:textColor="@color/white"
        android:textSize="15sp" />
</RelativeLayout>

感谢您的帮助。我有一种预感,这只是我正在查看的非常简单的事情。

【问题讨论】:

  • 它在做什么(或不做什么)?所以你什么都得不到?只有图形视图?

标签: java android xml android-linearlayout android-relativelayout


【解决方案1】:

Activity.findViewById 搜索绑定到活动的视图(通常是调用setLayout(R.layout.yourlayout); 的结果)。由于您没有调用它,因此当前没有活动视图,因此调用 findViewById 将返回 null。

看起来您正在尝试做的是从 xml 扩展布局,然后以编程方式将其添加到视图中。你应该做的是这样的:

Inflater inflater = LayoutInflater.from(context);
//this will inflate the mainRelativeLayout into the linear layout you called "ll"
inflater.inflate(R.id.mainRelativeLayout, ll);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多