2011-12-16 3 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日間検索し実験したが、何も働かない。ここで

は、カスタムScrollViewのクラス宣言です:

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つのボタンと1つのスピンナーが、上の横の行に正しく配置されています。しかしTimelineViewはその下に表示されません。私は、私が入れたLogCatでデバッグ出力を見ることができるので、TimelineViewが動作していることを知っています。

私は知恵があります。これは一般的な基本的な必要性のように思え、確かにGoogleのチュートリアルに値する。

+0

あなたは何が起こっているかについての詳細を教えてください。 – JoxTraex

答えて

0

私は、TimeLineのレイアウトの高さをxmlのfill_parentに変更するか、いくつかのコンテンツを追加する必要があると思います。

+0

私はそれを試して、今TimeLineViewが表示されますが、それはボタンを蹴って、全体のウィンドウを埋める。 – user1067564

2

LinearLayoutを使用していて、android:layout_height="fill_parent "を最初の要素に使用しているため、最初の要素が画面全体の高さを満たし、2番目の要素(com.projects.timeline2.TimelineView)のスペースがないため、代わりに

使用相対レイアウト。第二の要素を参照することはできません。そして、それは常に画面の湖底に滞在しにandroid:layout_aboveを置くことにより、第2の上記の最初の要素を整列させるように、第2の要素のためのandroid:layout_alignParentBottom="true"に言及最初の要素

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

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> 

私が正しい方向に向かうことを感謝します。

関連する問題