我想要一个水平滚动列表,其中包含一些带有标题的图片。 我使用TwoWayView(由lucasr)库,我无法显示任何图片。如果我有任何方法在这个库中有图像,请告诉我如何做到这一点。但如果它不支持图像,请建议我一个更好的图书馆。 我使用的是android studio v1.2,而且我是android的新手。
答案 0 :(得分:2)
为什么不使用orroids水平滚动视图而不是使用库,而是将图像添加到它。这是一个非常粗略的例子,您应该能够构建:
布局中的:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:layout_above="@+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_container"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
在您的代码中:
LinearLayout layout = (LinearLayout) view.findViewById(R.id.image_container)
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// Add 4 images
for (int i = 0; i < 4; i++) {
layoutParams.setMargins(20, 20, 20, 20);
layoutParams.gravity = Gravity.CENTER;
ImageView imageView = new ImageView(getActivity());
imageView.setImageResource(R.drawable.image);
imageView.setOnClickListener(documentImageListener);
imageView.setLayoutParams(layoutParams);
layout.addView(imageView);
}
就像我说的那样,非常粗糙,但希望它有所帮助!
编辑:顺便说一下布局方向应该是&#34;水平&#34;机器人:取向=&#34;水平&#34;&GT;
答案 1 :(得分:0)
对于水平滚动你不必使用TwoWayView Library,你可以像这样创建自定义水平布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
</HorizontalScrollView>
</LinearLayout>
在onCreate()方法中,从xml文件中获取linearLayout的id,并将动态创建的ImageView添加到linearlayout:
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
LinearLayout myGallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myGallery = (LinearLayout)findViewById(R.id.mygallery);
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/test/";
Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myGallery.addView(insertPhoto(file.getAbsolutePath()));
}
}
View insertPhoto(String path){
Bitmap bm = decodeSampledBitmapFromUri(path, 220, 220);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LayoutParams(250, 250));
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bm);
layout.addView(imageView);
return layout;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
}
答案 2 :(得分:0)
答案 3 :(得分:-1)
LinearLayout布局=(LinearLayout)findViewById(R.id.image_container); LinearLayout.LayoutParams layoutParams =新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//添加4张图片
for (int i = 0; i < 4; i++) {
layoutParams.setMargins(20, 20, 20, 20);
layoutParams.gravity = Gravity.CENTER;
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.bouton_admin);
//imageView.setOnClickListener(documentImageListener);
imageView.setLayoutParams(layoutParams);
layout.addView(imageView);
}