【发布时间】:2016-05-23 16:04:03
【问题描述】:
我正在尝试将水平 LinearLayout(带代码)放入垂直 LinearLayout(实际上位于 xml 文件中)。好吧,当我这样做时,它似乎不会创建水平线性,我发布我的代码和图像:
我的相机活动
package com.example.android.navigationdrawer;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MyCameraActivity extends AppCompatActivity {
private static final int CAMERA_REQUEST = 1888;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_my_camera);
ImageView photoButton = (ImageView) this.findViewById(R.id.button1);
//OnClick camera button
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
//Cast LinearLayout of .xml
LinearLayout CamaraLayout = (LinearLayout) findViewById(R.id.CameraLayout);
//Get rotation
int rotation = getWindowManager().getDefaultDisplay().getRotation();
//Number of photos
int PhotoNumber = 0;
//CREATE LINEAR LAYOUT
LinearLayout LL = new LinearLayout(this);
LL.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LL.setLayoutParams(LLParams);
int angle = 0;
switch (rotation) {
case Surface.ROTATION_90:
angle = -90;
PhotoNumber = PhotoNumber+1;
if (PhotoNumber == 6) {
//Set LinearLayout params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 5, 7, 5);
//Create imageView to put Bitmap
ImageView imageView = new ImageView(this);
//Put LayoutParams
imageView.setLayoutParams(lp);
//Adding the Bitmap
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Add image to Linear Layout
LL.addView(imageView);
//Add the Linear Layout to "CamaraLayout"
CamaraLayout.addView(LL);
} else {
//Set LinearLayout params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(7, 5, 7, 5);
//Create imageView to put Bitmap
ImageView imageView = new ImageView(this);
//Put LayoutParams
imageView.setLayoutParams(lp);
//Adding the Bitmap
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Add image to Linear Layout
CamaraLayout.addView(imageView);
}
break;
case Surface.ROTATION_180:
angle = 180;
PhotoNumber = PhotoNumber+1;
if (PhotoNumber == 4) {
//Set LinearLayout params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 5, 7, 5);
//Create imageView to put Bitmap
ImageView imageView = new ImageView(this);
//Put LayoutParams
imageView.setLayoutParams(lp);
//Adding the Bitmap
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Add image to Linear Layout
LL.addView(imageView);
//Add the Linear Layout to "CamaraLayout"
CamaraLayout.addView(LL);
} else {
//Set LinearLayout params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(7, 5, 7, 5);
//Create imageView to put Bitmap
ImageView imageView = new ImageView(this);
//Put LayoutParams
imageView.setLayoutParams(lp);
//Adding the Bitmap
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Add image to Linear Layout
CamaraLayout.addView(imageView);
}
break;
case Surface.ROTATION_270:
angle = 90;
PhotoNumber = PhotoNumber+1;
if (PhotoNumber == 6) {
//Set LinearLayout params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 5, 7, 5);
//Create imageView to put Bitmap
ImageView imageView = new ImageView(this);
//Put LayoutParams
imageView.setLayoutParams(lp);
//Adding the Bitmap
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Add image to Linear Layout
LL.addView(imageView);
//Add the Linear Layout to "CamaraLayout"
CamaraLayout.addView(LL);
} else {
//Set LinearLayout params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(7, 5, 7, 5);
//Create imageView to put Bitmap
ImageView imageView = new ImageView(this);
//Put LayoutParams
imageView.setLayoutParams(lp);
//Adding the Bitmap
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Add image to Linear Layout
CamaraLayout.addView(imageView);
}
break;
default:
angle = 0;
PhotoNumber = PhotoNumber+1;
if (PhotoNumber == 4) {
//Set LinearLayout params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 5, 7, 5);
//Create imageView to put Bitmap
ImageView imageView = new ImageView(this);
//Put LayoutParams
imageView.setLayoutParams(lp);
//Adding the Bitmap
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Add image to Linear Layout
LL.addView(imageView);
//Add the Linear Layout to "CamaraLayout"
CamaraLayout.addView(LL);
} else {
//Set LinearLayout params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(7, 5, 7, 5);
//Create imageView to put Bitmap
ImageView imageView = new ImageView(this);
//Put LayoutParams
imageView.setLayoutParams(lp);
//Adding the Bitmap
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Add image to Linear Layout
CamaraLayout.addView(imageView);
}
break;
}
}
}
}
我不发布 .xml,因为它只是一个 LinearLayout 和一个 ImageView。
我希望图像彼此相邻显示。
你们中的一些人可能已经想到了直接制作水平 LinearLayout 的想法,这很有效,但我正在开发一个可以放置不同“线条”照片的程序。
有什么想法吗?谢谢!
【问题讨论】:
标签: android