2012-11-25 19 views
10

Trong ứng dụng của tôi, tôi có ngăn kéo trượt với các nút hình ảnh trong đó và khi được nhấp, nó sẽ hiển thị mô tả và thông tin hình ảnh. Vì vậy, về cơ bản tôi chỉ sử dụng một tệp XML và một tệp Java cho điều này. (Nhưng tôi đã nhận thấy rằng việc thêm nhiều imagebutton và mages để hiển thị nó mất một thời gian để tải). Và giờ vì API 17 đang ngừng sử dụng ngăn kéo trượt khiến tôi lo lắng một chút về lượt tải xuống ứng dụng trong tương lai. Bây giờ câu hỏi của tôi là, có một cách khác để đạt được điều này mà không cần sử dụng ngăn kéo trượt hoặc spinner. Tôi không thực sự muốn tạo một tệp xml và java cho mỗi hình ảnh (tôi sẽ kết thúc với 100+ xml và của java) Đây là mã của tôi mà tôi có vào lúc này.Cách thay thế để thực hiện trình rút tiền hiện không được chấp nhận vì api 17

XML:

<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"> 

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

<ImageView 
    android:id="@+id/iM1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="fitStart" 
    android:adjustViewBounds="true"/> 

</RelativeLayout> 
</ScrollView> 

<SlidingDrawer 
    android:id="@+id/sD1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:content="@+id/content" 
    android:handle="@+id/handle"> 

    <Button 
     android:id="@+id/handle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon_1" /> 

    <RelativeLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/icon_background1"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

       <Button 
        android:id="@+id/asample" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@drawable/imageicon1"/> 
        ..... 

Và Java:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.campsites); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    final SlidingDrawer slider = (SlidingDrawer) findViewById(R.id.sD1); 
    final ImageView imageView = (ImageView) findViewById(R.id.iM1); 
    slider.animateOpen(); 

    Button next = (Button) findViewById(R.id.asample); 
    Button next1 = (Button) findViewById(R.id.bsample); 
    .......... 

    next.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 
      imageView.setImageDrawable(getResources().getDrawable(R.drawable.asample)); 
      slider.animateClose(); 
     } 
    }); 
    next1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 
      imageView.setImageDrawable(getResources().getDrawable(R.drawable.bsample)); 
      slider.animateClose(); 
     } 
    }); 
    ............ 

bất cứ ai có thể vui lòng giúp đỡ hoặc có một gợi ý về những gì để làm gì?

Trả lời

0

tôi nghĩ This là tốt thay thế
AFAIK nó không sử dụng SlidingDrawer và bạn có thể sửa đổi theo hướng ngăn

3

Đây là một SlidingDrawer từ cánh trái, đúng không? Nếu vậy, bạn có thể xem xét DrawerLayout.

Đây là một phần của Thư viện hỗ trợ Android và bạn sẽ có thể để thay thế XML của bạn với điều này thay vì khá đơn giản và tương thích ngược để API4

Từ trang đó, có một ví dụ.

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <!-- The navigation drawer --> 
    <ListView android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111"/> 
</android.support.v4.widget.DrawerLayout> 

Một số lưu ý từ rằng trang

bố trí này cho thấy một số đặc điểm quan trọng bố trí:

  • Quan điểm nội dung chính (FrameLayout ở trên) phải là đứa trẻ đầu tiên trong DrawerLayout vì thứ tự XML ngụ ý đặt hàng z và ngăn kéo phải ở trên cùng của nội dung. Chế độ xem nội dung chính được đặt là để phù hợp với chiều rộng và chiều cao của chế độ xem của phụ huynh, bởi vì nó đại diện cho toàn bộ giao diện người dùng khi ngăn điều hướng bị ẩn.
  • Chế độ xem ngăn kéo (ListView) phải chỉ định trọng số ngang của nó với thuộc tính android: layout_gravity. Để hỗ trợ ngôn ngữ (RTL) từ phải sang trái, hãy chỉ định giá trị bằng "bắt đầu" thay vì "trái" (vì vậy ngăn kéo xuất hiện ở bên phải khi bố cục là RTL).
  • Chế độ xem ngăn kéo chỉ định chiều rộng của nó theo đơn vị dp và chiều cao khớp với chế độ xem gốc. Chiều rộng ngăn kéo không được lớn hơn 320dp để người dùng luôn có thể xem một phần nội dung chính.

Chủ yếu là sự khác biệt là các DrawerLayout là cấp cao nhất và bạn đặt XML của bạn bên trong nó. Vì vậy, một cái gì đó như thế này (hoàn toàn chưa được kiểm tra):

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- your content surrounded by a layout to signify that it's actually content --> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ScrollView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:id="@+id/iM1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:scaleType="fitStart" 
        android:adjustViewBounds="true"/> 

      </RelativeLayout> 
     </ScrollView> 
    </RelativeLayout> 
    <!-- your sliding menu in its own layout --> 
    <LinearLayout 
     android:layout_gravity="start" 
     android:layout_width="240dp" 
     android:layout_height="wrap_content"> 
     <Button 
      android:id="@+id/handle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/icon_1" /> 

     <RelativeLayout 
      android:id="@+id/content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/icon_background1"> 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 

        <Button 
         android:id="@+id/asample" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@drawable/imageicon1"/> 
         ..... 
    </LinearLayout> 
</android.support.v4.widget.DrawerLayout> 
1

Tôi thà đề xuất một menu trượt đơn giản mà tôi tự tạo ra.

khái niệm tôi được sử dụng

trượt nút và nội dung bảng

ban đầu thanh trượt nút là sang bên trái (trong ví dụ của tôi), khi bạn click vào nó, nó thay đổi và khung nội dung được thực hiện có thể nhìn thấy

làm thế nào tôi achieived này

tôi chơi với lề trái, vì vậy khi bạn nhấn vào nút thanh trượt các conte nt pane (ẩn ban đầu) trở thành rộng như screen_width/3, và khi bạn nhấn nó một lần nữa nó ẩn ..

heres mã của tôi với nó.

public class MainActivity extends Activity { 
    boolean toggle_open=false; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 




    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void open(View v){ 
     switch (v.getId()) { 
     case R.id.imageButton1: 
      if(!toggle_open){ 
      RelativeLayout header=(RelativeLayout)findViewById(R.id.header); 
      Display size=getWindow().getWindowManager().getDefaultDisplay(); 
      int widthby2=size.getWidth()/3; 
      RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 
      lp.setMargins(size.getWidth()/2, 0, 0, 0); 
      header.setLayoutParams(lp); 

      RelativeLayout slider=(RelativeLayout)findViewById(R.id.panel); 
      slider.setLayoutParams(new RelativeLayout.LayoutParams((size.getWidth()/2),size.getHeight())); 
      slider.setVisibility(View.VISIBLE); 

      toggle_open=true; 
      } 
      else{ 
       RelativeLayout header=(RelativeLayout)findViewById(R.id.header); 
       RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 
       lp.setMargins(0, 0, 0, 0); 
       header.setLayoutParams(lp); 

       RelativeLayout slider=(RelativeLayout)findViewById(R.id.panel); 
       slider.setVisibility(View.GONE); 
       toggle_open=false; 

      } 
      break; 

     default: 
      break; 
     } 
    } 



} 

// xml đang

<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" 
    tools:context=".MainActivity" > 

    <RelativeLayout 
     android:id="@+id/header" 
     android:background="@android:color/black" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:padding="20dp" > 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:background="@android:color/black" 
      android:onClick="open" 
      android:src="@android:drawable/ic_dialog_dialer" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/imageButton1" 
      android:layout_toRightOf="@+id/imageButton1" 
      android:text="Admin Panel" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="@android:color/white" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/panel" 
     android:visibility="gone" 
     android:layout_toLeftOf="@+id/header" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:background="@android:color/darker_gray" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 

     <fragment class="com.example.avanse.FragmentLayout$TitlesFragment" 
      android:id="@+id/titles" 
      android:layout_width="match_parent" android:layout_height="match_parent"/> 

    </RelativeLayout> 

</RelativeLayout> 
0

android đơn giản bạn có thể sử dụng DrawerLayout. nhưng tôi khuyên bạn nên SlidingMenu lib những gì có khả năng sử dụng tốt hơn cho người dùng và lập trình viên.

+0

DrawerLayout cũng hoạt động với các phiên bản Android cũ là nó nằm trong thư viện hỗ trợ v4. – NasaGeek

+0

bạn nói đúng - tôi sẽ sửa câu trả lời của tôi để sửa nó –

1

SlidingUpPanel từ các chàng trai của ứng dụng Umano có vẻ là cách tốt nhất ngay bây giờ. Bạn có thể tìm thấy nó trong: https://github.com/umano/AndroidSlidingUpPanel

Tôi tìm thấy thông tin về nó trong SOF bài khác này: vertical DrawerLayout or SlidingPaneLayout : D

Edit: Cái này cũng có vẻ rất hứa hẹn: https://github.com/6wunderkinder/android-sliding-layer-lib (trong youtube video dường như làm việc chỉ từ phải sang trái và trái sang phải, nhưng nếu bạn tải xuống ứng dụng demo thực tế, bạn sẽ thấy rằng nó cũng có thể đi từ dưới lên trên và từ trên xuống dưới)

Các vấn đề liên quan