2014-11-05 4 views
0

У меня есть макет, как показано ниже в коде xml. Внутри ScrollView Я хочу приложить LinearLayouts, который позже будет содержать ImageView и небольшую текстовую строку под ним. Это, я должен делать программно. Пока загружается изображение, LinearLayout должен показывать неопределенный ProgressBar, который удаляется перед изображением и добавляется текст. Все это делается в AsyncTasks.Содержимое ScrollView вне границ родителя

fragment_images.xml:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" 
    android:gravity   = "center" 
    android:layout_height = "match_parent" 
    android:layout_width = "match_parent" > 


    <ScrollView 
     android:id    = "@+id/scrollview_fragment_species_images_vertical" 
     android:layout_height = "wrap_content" 
     android:layout_width = "wrap_content" 
     android:orientation  = "vertical" 
     android:visibility  = "gone" > 

     <LinearLayout 
      android:layout_gravity = "center" 
      android:id    = "@+id/linlay_fragment_species_images_vertical" 
      android:layout_height = "wrap_content" 
      android:layout_width = "wrap_content" 
      android:orientation  = "vertical" 
      android:visibility  = "gone" > 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

Это код, который загружает изображения, добавляет и заполнит LinearLayouts:

ScrollView scrollViewVert = (ScrollView) getActivity().findViewById(R.id.scrollview_fragment_species_images_vertical); 
scrollViewVert.setVisibility(View.VISIBLE); 
LinearLayout linlayContainer = (LinearLayout) getActivity().findViewById(R.id.linlay_fragment_species_images_vertical); 
linlayContainer.setVisibility(View.VISIBLE); 

private void makeImageList(String jsonImages) { 

    // ... 

    for (int i=0; i<count_images; i++) { 
     final ProgressBar progress = new ProgressBar(getActivity().getBaseContext()); 
     progress.setIndeterminate(true); 


     final LinearLayout childContainer = new LinearLayout(getActivity().getBaseContext()); 
     childContainer.setGravity(Gravity.CENTER_HORIZONTAL); 
     childContainer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     childContainer.setOrientation(LinearLayout.VERTICAL); 
     childContainer.setPadding(15, 15, 15, 15); 


     final TextView childText = new TextView(getActivity().getBaseContext()); 
     childText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     childText.setPadding(15, 10, 15, 5); 
     String imageSize = (Integer.parseInt(templist.get(i)[2])/1024) + "kB"; 
     childText.setText(imageSize); 
     childText.setTextColor(Color.BLACK); 


     final ImageView child = new ImageView(getActivity().getBaseContext()); 
     child.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     child.setPadding(15, 15, 15, 0); 


     URL url = null; 
     try {url = new URL(address);} // just an internet address. this works! 
     catch (MalformedURLException e) {e.printStackTrace();} 

     linlayContainer.addView(childContainer); 
     childContainer.addView(progress); 


     new AsyncTask<URL, Void, Bitmap>() { 
      @Override 
      protected Bitmap doInBackground(URL... params) { 
       Bitmap image = null; 
       try {image = BitmapFactory.decodeStream(params[0].openConnection().getInputStream());} 
       catch (IOException e) {e.printStackTrace();} 

       return image; 
      } 

      protected void onPostExecute(Bitmap image) { 
       child.setImageBitmap(image); 
       childContainer.removeView(progress); 
       childContainer.addView(child); 
       childContainer.addView(childText); 
      } 
     }.execute(url); 
    } 
} 

Как-то мой макет смещается по вертикали, и я не понимаю Зачем. На приведенных ниже изображениях показана проблема. Влево верхний предел ScrollView достигается при отсутствии одного изображения; правый - нижний конец ScrollView со слишком большим количеством свободного места. Возможно, это не имеет значения, но это активность с вкладками. Я не знаю, как я могу сузить эту ошибку.

Заранее спасибо.

Upper limit of ScrollView Lower limit of ScrollView

ответ

0

Я нашел ошибку, хотя я не понимаю, что именно здесь не так:

<LinearLayout 
     android:layout_gravity = "center" 
... 

LinearLayout внутри ScrollView должны быть сосредоточены только по горизонтали, а не по горизонтали и по вертикали. Таким образом, линия выше должна быть исправлена, как, например:

 android:layout_gravity = "center_horizontal" 

Если кто-то будет так добры и объясните, почему layout_gravity параметрическое не ограничивается родительской точки зрения, но, видимо, на весь экран или любой другой корень вида это ... Я этого не понимаю.

Смежные вопросы