2014-03-25 43 views
1

對話的Java代碼創建警報對話框:如何使用圖像和滾動[ANDROID]

final Dialog dialog = new Dialog(context); 
       dialog.setContentView(R.layout.custom_dialog); 
       dialog.setTitle("Title..."); 
       ScrollView scroll = new ScrollView(context); 
       scroll.setBackgroundColor(android.R.color.transparent); 
       scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
                  LayoutParams.FILL_PARENT));  
       // set the custom dialog components - text, image and button 
       TextView text = (TextView) dialog.findViewById(R.id.text); 
       text.setText("Android custom dialog example!"); 
       ImageView image = (ImageView) dialog.findViewById(R.id.image); 
       image.setImageResource(R.drawable.icon); 
       dialog.show(); 

XML代碼這裏:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/layout_root" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dp" 
      android:scrollbarAlwaysDrawVerticalTrack="true"> 
      > 
<ImageView android:id="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginRight="10dp" 
      /> 
<TextView android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:textColor="#FFF" 
      /> 

我知道,如果我只有文字上警報對話框自動滾動,但使用ImageView自動滾動不起作用。 我該如何解決這個問題?

回答

3

把你的佈局的滾動型內:

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

     <LinearLayout 
      android:id="@+id/layout_root" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:scrollbarAlwaysDrawVerticalTrack="true" > 

      <ImageView android:id="@+id/image" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_marginRight="10dp" /> 

      <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:textColor="#FFF" /> 

    </LinearLayout> 

<ScrollView> 

我看到你的LinearLayouthorizontal方向。所以,如果這種情況下,你想要水平滾動,你應該替換ScrollViewHorizontalScrollView並將方向設置爲水平。
讓我知道這是否工作。

+0

thx現在工作;) – OmgDatProblem

+0

好看,很高興幫助!謝謝。 – Fllo