2016-06-06 5 views
0

내가이 보이는 여기 방법, 다음 줄에 4, 5 이미지를 배치하기 위해 수행해야비정상적으로 이미지가 화면 밖으로 이동

enter image description here

4, 5 이미지는 어떻게 수정 화면에서 이동 ?

MainActivity :

LayoutInflater l = getLayoutInflater(); 
LinearLayout ll = (LinearLayout) findViewById(R.id.main); 


Integer odpowiedzi[] = {R.drawable.kwiaty1, R.drawable.kwiaty2, R.drawable.kwiaty3, R.drawable.kwiaty4, R.drawable.kwiaty5}; 

for (Integer odp : odpowiedzi) { 
    View v = l.inflate(R.layout.activ2, null); 
    ImageView b = (ImageView) v.findViewById(R.id.imageView6); 
    b.setImageResource(odp); 
    ll.addView(v); 

activ2.xml :

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

    <ImageView 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:id="@+id/imageView6"/> 
</LinearLayout> 

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/main"> 
</LinearLayout> 
+0

해결할 수있는 방법에는 여러 가지가 있습니다. 그러나 간단하고 자동적 인 방법으로 그것을 수행하고 그냥 감쌀 수 없습니다. 다른 레이아웃과 어댑터가 필요할 것입니다. 그러나 그것은 실제로 가지고있는 시나리오의 종류에 달려 있습니다. – xklakoux

+0

가능한 중복의 [안드로이드 수평 LinearLayout - 요소를 래핑] (http://stackoverflow.com/questions/14528381/android-horizontal-linearlayout-wrap-elements) – xklakoux

+0

나는 내 대답은 –

답변

0

하는 선형 레이아웃 기본 레이아웃 확인 수직 정렬

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/main"> 
    </LinearLayout> 
,451,515,

이제 화면 폭 메인 레이아웃의 또 다른 수평 배향 선형 배열을 추가하고 그 안에 imageViews 추가 500dp보다 크면이

GET 화면 폭

public static int getScreenWidth() { 
    return Resources.getSystem().getDisplayMetrics().widthPixels; 
} 

public static float convertDpToPixel(float dp, Context context){ 
    Resources resources = context.getResources(); 
    DisplayMetrics metrics = resources.getDisplayMetrics(); 
    float px = dp * ((float)metrics.densityDpi/DisplayMetrics.DENSITY_DEFAULT); 
    return px; 
} 

같은 동적 레이아웃을 추가 처리한다.

다른 경우 화면 너비가 500dp보다 작은 경우 주 레이아웃에서 첫 번째 가로 정렬 선형 레이아웃을 추가하고 그 안에 imageViews를 추가하십시오. 그리고 메인 레이아웃의 또 다른 수평 정렬 된 선형 레이아웃을 추가하고

// Create new LinearLayout 
    LinearLayout linearLayout = new LinearLayout(this); 
    linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.MATCH_PARENT)); 
    linearLayout.setOrientation(LinearLayout.HORIZONTAL); 

    // Add textviews 
    TextView textView1 = new TextView(this); 
    textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    textView1.setText("programmatically created TextView1"); 
    textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB 
    textView1.setPadding(20, 20, 20, 20); // in pixels (left, top, right, bottom) 
    linearLayout.addView(textView1); 

    TextView textView2 = new TextView(this); 
    LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT); 
    layoutParams.gravity = Gravity.RIGHT; 
    layoutParams.setMargins(10, 10, 10, 10); // (left, top, right, bottom) 
    textView2.setLayoutParams(layoutParams); 
    textView2.setText("programmatically created TextView2"); 
    textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); 
    textView2.setBackgroundColor(0xffffdbdb); // hex color 0xAARRGGBB 
    linearLayout.addView(textView2); 

    // Set context view 
    mainLinearLayout.addView(linearLayout); 
0

부모에 맞게 android:adjustViewBounds="true"를 추가 이미지 뷰에서 0 픽셀 수 activ2와 무게 1. 에있는 LinearLayout의 폭을 변경하려고 거기에 imageViews을 유지 추가 할 수 있습니다. activ1에서 너비를 match_parent로 일부 채우기로 변경하십시오.

그런 식으로 이미지 크기가 분산되고 부모와 일치해야합니다.

0

와 시도 :

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

<ImageView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView6" 
    android:adjustviewbounds="true" 
    android:layout_weight="1"/> 
</LinearLayout> 

그리고 활동

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/main" 
android:weightsum="NUMBER_OF_OBJECTS" > 
</LinearLayout> 

는 희망이 도움이!

관련 문제