2012-01-14 31 views
2

我有一種情況,我需要創建一個左側面板。我的經理說這應該與電視中使用的電視相同電影 Google TV設備上的應用程序,它最初只顯示圖標,並在右側顯示圖標文字時顯示其內容。創建Android左側面板,就像在谷歌電視「電視和電影」應用程序中的一個

我想標籤是像下面的圖片(白色箱子是我的鏈接和紅色ractangle是我左圖)在:

Required Left Panel Structure

放大的圖像可以被看作here

我對左面板的要求是:

  1. 它應該擴展到正確的s當它的任何一個鏈接被聚焦時,這個鏈接就會被放置。我的動畫寬度不應該很難。但我怎麼知道一個控件是左面板有/失去焦點
  2. 當在左面板中單擊任何鏈接時,將單獨的活動加載到右側的FrameLayout中......我也使用解決方法。但我寧願知道一些體面的好方法。
  3. 左側面板的寬度應該減小,並且當用戶通過按下右側D-PAD按鈕來聚焦加載的活動時應該隱藏圖標的文本。

  4. 我必須可自由能使用左面板和在該容器的FrameLayout加載的子活動控制之間KEYBOARD導航。這是因爲我的應用程序適用於Google TV,用戶可以使用D-Pad進行導航。 這是我面臨的主要問題......由於我無法從子控件到其父的鄰居控制導航

要具有上述所有的點,我看着TabHost卻苦於找不到讓它看起來像我需要的那種方式。

我也看過themissingtabwidget,但它的垂直製表符填充了所有垂直可用空間,並且在那裏放置對我來說是不可控的,我需要更多像左側面板一樣的結構,我可以在任何地方放置鏈接。所以這也失敗了。

現在我決定自己創建一個。 我這樣做的方式如下:

  1. main.xml中包含的FrameLayout在根持有
    • 的FrameLayout舉辦活動 - 我從左邊,這樣設置其利潤率下一個左側面板將適合該空間&何時將覆蓋在它下面的框架,當我動畫面板的大小
    • 相對eLayout用於放置左側面板項目。

main.xml中如下:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<!-- 
    THIS First TabHost IS NOT USED AT ALL. 
    I JUST PUT IT HERE AS THE underlying TabActivity required it and was giving me error. 
    You can see its visibility is set to GONE. 

    You can just minimize it and forget it as its a silly Workaround. 
    and help me if you know of some better way 
--> 
<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="gone" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="100dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="0" 
      android:orientation="vertical" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</TabHost> 

<!-- 
    Activities are loaded here in this frame. 
    Its margin from Left is set to 150dp to have space. 
    Its placed on the first place as the second RelativeLayout would be used as Left Panel. 
--> 
<FrameLayout 
    android:id="@+id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginLeft="150dp" 
    android:nextFocusLeft="@+id/leftpanel" 
    android:background="#CCAAAA" > 
</FrameLayout> 

<!-- 
    Left Panel holding links/buttons etc to load a separate activity in above FrameLayout 
--> 
<RelativeLayout 
    android:id="@+id/leftpanel" 
    android:layout_width="200dp"   
    android:layout_height="fill_parent" 
    android:background="#33FAFAFA" 
    android:focusable="True" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/btnArtists" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Artists" > 
    </Button> 

    <Button 
     android:id="@+id/btnAlbum" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/btnArtists" 
     android:text="Album" > 
    </Button> 

    <Button 
     android:id="@+id/btnLogin" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="True" 
     android:text="Login" > 
    </Button> 
</RelativeLayout> 

主選項卡活性如下:

public class TestActivity extends TabActivity { 
FrameLayout container; 
Intent artistsIntent; 
Intent albumsIntent; 

/** Called when the activity is first created. */ 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    container = (FrameLayout)findViewById(R.id.container); 
    artistsIntent = new Intent().setClass(this, ArtistsActivity.class); 
    albumsIntent = new Intent().setClass(this, AlbumsActivity.class); 

    Button btnArtist = (Button)findViewById(R.id.btnArtists);   
    btnArtist.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      container.removeAllViews(); 
      View view = getLocalActivityManager().startActivity("ArtistsActivity", 
        new Intent(getApplicationContext(), ArtistsActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
      container.addView(view);     
     } 
    }); 

    Button btnlbum = (Button)findViewById(R.id.btnAlbum); 
    btnlbum.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      container.removeAllViews(); 
      View view = getLocalActivityManager().startActivity("AlbumsActivity", 
        new Intent(getApplicationContext(), AlbumsActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
      container.addView(view); 

     } 
    }); 
}  
} 

作爲您可以看到主要的TestActivity是一個TabActivity,它提供了實現選項卡的功能。我從TabActivity擴展我的活動,因爲根據我的知識,這是將活動添加到FrameLayout作爲子視圖的唯一方法。

這也迫使我有一個具有該特定名稱的TabHost。所以我添加它並將其可見性設置爲不見了以使其停止抱怨。 ...... 是我知道...這是一個愚蠢的替代方法 ..但我就不會在這裏如果我知道一些適當的方式......所以幫助我,如果有一些更好的方式來做到這一點。

我目前的測試項目中的另一個問題是,當我在以的FrameLayout使用右箭頭鍵加載的子活動焦點的按鈕。我不能回用LEFT集中在左側面板中的按鈕鍵盤鍵...

HOW TO DO IT ????????????

我已經上傳小樣本項目here。請下載並查看它。同時在AndroidManifest.xml中將.TestActivity的主題設置爲android:theme =「@ android:style/Theme.Holo」,以按照預期在全屏中查看應用。

你的幫助是極大的讚賞。

注意:我正在爲Google TV創建此應用程序...導航主要依賴於D-Pad ...所以我必須確保使用Kayboard可以到達任何位置。

幫幫我吧。

回答

1

Android碎片對您的目的來說是完美的。看看documentation,這真的很清楚,並解釋如何正確使用它。

相關問題