简体   繁体   English

为什么没有同时显示我的两个视图?

[英]Why aren't both of my views being displayed?

So I've been trying to figure out layouts and using the layout inflater, but I'm running into some issues. 因此,我一直在尝试找出布局并使用布局填充器,但是我遇到了一些问题。 I have two relative xml layout files, the one xml file has two textViews, an editText field, and a spinner object: 我有两个相对的xml布局文件,一个xml文件有两个textViews,一个editText字段和一个Spinner对象:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/firstLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

<TextView
    android:id="@+id/goalNameView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dip"
    android:text="@string/goalName" />

<EditText
    android:id="@+id/goal_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/goalNameView"
    android:hint="@string/editText"
    android:ems="10" >

    <requestFocus />
</EditText>

 <TextView
    android:id="@+id/goalTasksView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/goal_tasks"
    android:layout_alignBottom="@+id/goal_tasks"
    android:layout_alignLeft="@+id/goalNameView"
    android:text="@string/goalTasks" />

<Spinner
    android:id="@+id/goal_tasks"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/goalTasksView"
    android:layout_marginTop="51dp" />


 </RelativeLayout>

the other xml file has one textView and one editText field: 另一个xml文件具有一个textView和一个editText字段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tView"
 >
<EditText
    android:id="@+id/taskNameField"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/taskNameView"
    android:ems="10"
    android:hint="@string/editText" />

<TextView
    android:id="@+id/taskNameView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/taskNameField"
    android:layout_alignBottom="@+id/taskNameField"
    android:layout_alignParentLeft="true"
    android:text="@string/taskName"
     />

</RelativeLayout>

I'm trying to patch the two layouts together with a third xml file (main.xml): 我正在尝试将两个布局与第三个xml文件(main.xml)一起打补丁:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/theMain"
>

<include android:id="@id/mainLayout" layout="@layout/activity_goal_keeper"  />



</LinearLayout>

The way I'm looking to have the layout work is to display the first layout (@id/firstLayout) and then the second layout (@id/tView) directly beneath it. 我希望进行布局工作的方式是在其正下方显示第一个布局(@ id / firstLayout),然后显示第二个布局(@ id / tView)。 I've read that I need to implement the layouts on a LinearLayout to achieve this which is why I have the third layout (@id/theMain). 我读过,我需要实现上的LinearLayout布局来实现这个这就是为什么我有第三轮廓(@ ID / theMain)。

As you can see I have an include statement for @id/firstLayout, but I don't have an include statement for @id/tView because I'm trying to inflate multiple versions of @id/tView via my main activity: 如您所见,我有一个@ id / firstLayout的include语句,但我没有@ id / tView的include语句,因为我试图通过我的主要活动来充实@ id / tView的多个版本:

 public class GoalKeeperActivity extends Activity implements       AdapterView.OnItemSelectedListener {
 public Integer[] items= {1,2,3,4,5,6,7,8};

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //setContentView(R.layout.activity_goal_keeper);
    setContentView(R.layout.main);

    Spinner numOfTasks=(Spinner) findViewById(R.id.goal_tasks);
    numOfTasks.setOnItemSelectedListener(this);

    ArrayAdapter<Integer> aa = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, items);

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    numOfTasks.setAdapter(aa);

    ViewGroup item = (ViewGroup) findViewById(R.id.theMain);

    for(int i=0; i<3;i++)
    {
      View child = getLayoutInflater().inflate(R.layout.tasks_view, item, false);
      child.setId(i);
      item.addView(child);

    }

My problem is that @firstLayout displays properly, but @tView doesn't show up at all. 我的问题是@firstLayout可以正确显示,但是@tView根本不显示。 Can someone please explain what I'm missing? 有人可以解释我所缺少的吗?

Thanks in advance. 提前致谢。

I figured it out, it was simply an issue of setting the "android:layout_height" attribute equal to wrap_content on the @id/tView xml file. 我知道了,这仅仅是在@ id / tView xml文件上将“ android:layout_height”属性设置为等于wrap_content的问题。 Silly little mistakes... 愚蠢的小错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM