2011-12-16 135 views
0

我有一个自定义的ScrollView,如果它是模拟器中唯一的东西,它可以正常工作。自定义滚动视图不出现

例如,这工作得很好:

public class Timeline2Activity extends Activity { 
     private TimelineView tlv; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      tlv = new TimelineView(this); 
      setContentView(tlv); 
     } 
    } 

但是,如果我先添加小部件则没有出现。我已经搜索了2天,并且没有任何效果。

这是自定义滚动型的类声明:

public class TimelineView extends ScrollView implements OnTouchListener { 

而且这里的构造函数:

public TimelineView(Context context) { 
    super(context); 
this.setOnTouchListener(this); 
setVisibility(VISIBLE); 
} 
//the following constructor is needed to inflate the view from XML 
public TimelineView(Context context, AttributeSet ats) { 
    super(context,ats); 
this.setOnTouchListener(this); 
setVisibility(VISIBLE); 
} 

下面是main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/zoomall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Zoom All" /> 
    <Button 
     android:id="@+id/zoomout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="-" /> 
    <Button 
     android:id="@+id/zoomin" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="+" /> 
    <Spinner 
     android:id="@+id/category_spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:prompt="@string/category_prompt" 
    /> 
    </LinearLayout> 

    <com.projects.timeline2.TimelineView 
     android:id="@+id/timeline_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

这里是主程序:

public class Timeline2Activity extends Activity { 
    private TimelineView tlv; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //tlv = new TimelineView(this); 
     //setContentView(tlv); 
     setContentView(R.layout.main); 

     Spinner spinnerCategory = (Spinner) findViewById(R.id.category_spinner); 
     ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(
       this, R.array.categories, android.R.layout.simple_spinner_item); 
     adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinnerCategory.setAdapter(adapterCategory); 
     spinnerCategory.setOnItemSelectedListener(new MyOnItemSelectedListener()); 
     //TimelineView tlv = (TimelineView) findViewById(R.id.timeline_view); 
     //ViewGroup container = (ViewGroup) findViewById(R.id.container); 
     //container.addView(tlv); 
    } 
    public class MyOnItemSelectedListener implements OnItemSelectedListener { 

     public void onItemSelected(AdapterView<?> parent, 
      View view, int pos, long id) { 
      Toast.makeText(parent.getContext(), "The selection is " + 
       parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); 
     } 

     public void onNothingSelected(AdapterView parent) { 
      // Do nothing. 
     } 
    } 
} 

注释掉的行是我尝试过的东西,但也没有工作。

此代码正确地将3个按钮和一个微调器放在顶部的水平行中。但TimelineView无法显示在它们下面。我知道TimelineView正在运行,因为我可以在我输入的LogCat中看到调试输出。

我在我的智慧结尾,并希望有人能说出点亮。这似乎是一个普遍的基本需求,当然值得一个谷歌教程。

+0

您能否提供更多关于发生的事情的信息? – JoxTraex 2011-12-16 04:28:23

回答

0

我认为你应该将TimeLine的布局高度更改为xml中的fill_parent,或者向其中添加一些内容。

+0

我试过了,现在TimeLineView显示出来了,但它填满了整个窗口,踢出按钮。 – user1067564 2011-12-16 16:02:28

2

您正在使用LinearLayout,并且您已经使用了android:layout_height="fill_parent“到第一个元素,因此第一个元素将填充整个屏幕高度,第二个元素(com.projects.timeline2.TimelineView)将没有空间,这就是为什么您无法看到第二个元素。

使用相对布局,而不是,并提android:layout_alignParentBottom="true"的第二个元素,使其始终保持在屏幕的BUTTOM并把android:layout_above对齐到上述的第二个第一要素第一个元素

您的XML将是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 



    <com.projects.timeline2.TimelineView 

     android:id="@+id/timeline_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     /> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:layout_above="@+id/timeline_view"> 

    <Button 
     android:id="@+id/zoomall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Zoom All" /> 
    <Button 
     android:id="@+id/zoomout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="-" /> 
    <Button 
     android:id="@+id/zoomin" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="+" /> 
    <Spinner 
     android:id="@+id/category_spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:prompt="category_prompt" 
    /> 
    </LinearLayout> 
</RelativeLayout> 
+0

我把你的main.xml替换成了你的,但同样的事情发生了。 TimelineView无法显示在按钮下方。然后我回去修改主程序,只包含TimelineView,就像以前一样,它的工作很完美,所以我不知道它是错的。任何其他想法? – user1067564 2011-12-16 15:13:02

0

我终于明白了。这是main.xml的工作。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/buttons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_alignParentTop="true" 
    > 

    <Button 
     android:id="@+id/zoomall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Zoom All" /> 
    <Button 
     android:id="@+id/zoomout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="-" /> 
    <Button 
     android:id="@+id/zoomin" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="+" /> 
    <Spinner 
     android:id="@+id/category_spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    /> 
    </LinearLayout> 

    <com.projects.timeline2.TimelineView 
     android:id="@+id/timeline_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_below="@id/buttons" 
     /> 
</RelativeLayout> 

感谢大家让我朝着正确的方向前进!