0

Я программирую создание простого LinearLayout, у которого два ребенка (Imageviews) занимает равную ширину. В этом случае Rootview является CustomLayout (SquareLinearLayout).Программно добавление ImageViews в LinearLayout

Вот SquareLinearLayout:

public class SquareLinearLayout extends LinearLayout { 

    public SquareLinearLayout(Context context) { 
     super(context); 
    } 

    public SquareLinearLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int orientation = getContext().getResources().getConfiguration().orientation; 

     if (orientation == Configuration.ORIENTATION_PORTRAIT || orientation == Configuration.ORIENTATION_UNDEFINED) { 

      int width = MeasureSpec.getSize(widthMeasureSpec); 
      int height = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); 
      setMeasuredDimension(width, height); 

     } else { 

      int height = MeasureSpec.getSize(heightMeasureSpec); 
      int width = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 
      setMeasuredDimension(width, height); 

     } 

    } 
} 

Связанные XML

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"> 

    <com.customviews.SquareLinearLayout 
     android:id="@+id/lay_square" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ececec" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/lay_r_capture" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 
     </LinearLayout> 

    </com.customviews.SquareLinearLayout> 

</LinearLayout> 

Программным Добавление ImageView в LinearLayout

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture); 
LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
lay_r_capture.setOrientation(LinearLayout.HORIZONTAL); 
lay_r_capture.setWeightSum(2f); 
lay_r_capture.setLayoutParams(layparam); 

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1f); 
    lp.setMargins(8,8,8,8); 

ImageView img1 = new ImageView(this); 
Bitmap photo1 = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.photo_1); 
img1.setLayoutParams(lp); 
img1.setImageBitmap(photo1); 

ImageView img2 = new ImageView(this); 
Bitmap photo2 = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.photo_2); 
img2.setLayoutParams(lp); 
img2.setImageBitmap(photo2); 

lay_r_capture.addView(img1); 
lay_r_capture.addView(img2); 

Это вещи не работают, как ожидается, какthis

Выход:

enter image description here

Какие атрибуты или LayoutParams я не хватает?

+0

, что ваш вопрос? – rsc

+0

В чем проблема? –

+0

он не работал, как ожидалось, позвольте мне изменить вопрос с выходным изображением. –

ответ

0

Проблема, с которой вы сталкиваетесь, связана с SquareLinearLayout. В onMeasure() из SquareLinearLayout вы меняете ширину и высоту представления для достижения макета квадратного типа.

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int orientation = getContext().getResources().getConfiguration().orientation; 

     //The below code will set width = height, to achieve square layout 
     if (orientation == Configuration.ORIENTATION_PORTRAIT || orientation == Configuration.ORIENTATION_UNDEFINED) { 

      int width = MeasureSpec.getSize(widthMeasureSpec); 
      int height = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); 
      setMeasuredDimension(width, height); 

     } else { 

      int height = MeasureSpec.getSize(heightMeasureSpec); 
      int width = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 
      setMeasuredDimension(width, height); 

     } 
+0

Спасибо за ваши усилия, но я хочу SquareLinearLayout в качестве корневого макета для достижения моего результата! –

0

Вы Programatically настройки MATCH_PARENT как высота здесь:

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture); 
LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
lay_r_capture.setOrientation(LinearLayout.HORIZONTAL); 
lay_r_capture.setWeightSum(2f); 
lay_r_capture.setLayoutParams(layparam); 

Так изменить его WRAP_CONTENT как:

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture); 
LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
lay_r_capture.setOrientation(LinearLayout.HORIZONTAL); 
lay_r_capture.setWeightSum(2f); 
lay_r_capture.setLayoutParams(layparam); 
1
//you can set imageView like //img1.setImageResource(R.drawable.photo_2);  

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture); 
    LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
    lay_r_capture.setOrientation(LinearLayout.HORIZONTAL); 
    lay_r_capture.setWeightSum(2f); 
    lay_r_capture.setLayoutParams(layparam); 

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1f); 
     lp.setMargins(8,8,8,8); 

    ImageView img1 = new ImageView(this); 
    img1.setImageResource(R.drawable.photo_1); 
    img1.setLayoutParams(lp); 

    ImageView img2 = new ImageView(this); 
    img2.setImageResource(R.drawable.photo_2); 
    img2.setLayoutParams(lp); 

    lay_r_capture.addView(img1); 
    lay_r_capture.addView(img2); 
Смежные вопросы