我的应用程序需要显示来自主机网址的图片。我将图像添加到类文件的内部布局中。但我无法缩小图像尺寸,因为您可以看到它占据整个屏幕。
import beans.MenuItem;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ImageLoading extends Activity {
public static final String PHOTOS_BASE_URL =
"http://services.hanselandpetal.com/photos/";
final JSONObject cartinproduct= new JSONObject();
@Override
protected void onCreate(Bundle icicle) {
setContentView(R.layout.imageloding);
super.onCreate(icicle);
Button display=(Button) findViewById(R.id.button1);
display.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
updateDisplay();
}
});
}
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void updateDisplay() {
Log.v("updateDisplay", "Started");
setContentView(R.layout.imageloding);
final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
final MenuController Controller = (MenuController) getApplicationContext();
MenuItem MenuObject = new MenuItem();
Log.v("loop", "Started");
for(int i=0;i<3;i++)
{
MenuObject.setMenu_name("Product "+i);
Controller.setProducts(MenuObject);
}
//Product arraylist size
int ProductsSize = Controller.getProductsArraylistSize();
// create the layout params that will be used to define how your
// button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Log.v("for loop", "Started");
for(int j=0;j< ProductsSize;j++)
{
// Get probuct data from product data arraylist
String pName = Controller.getProducts(j).getMenu_name();
// Create LinearLayout to view elemnts
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
Log.v("inner loop", "Started");
com.example.testapp.LoaderImageView image=new LoaderImageView(this, "http://developer.android.com/images/dialog_buttons.png");
image.setImageDrawable("http://developer.android.com/images/MENU_MNU105.jpg");
ll.addView(image);
TextView product = new TextView(this);
product.setText(" "+pName+" ");
ll.addView(product);
lm.addView(ll);
}
}
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/display" />
<LinearLayout
android:id="@+id/linearMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
答案 0 :(得分:0)
您可以在XML中添加com.example.testapp.LoaderImageView作为LinearLayout的子级并定义它的维度,或者根据您的需要维度动态添加LinearLayout.LayoutParams。 在com.example.testapp.LoaderImageView image = new LoaderImageView(this,&#34; http://developer.android.com/images/dialog_buttons.png&#34;);
之后添加此行int desire_width = 50;
int desire_height = 50;
image.setLayoutParams(new LinearLayout.LayoutParams(desire_width, desire_height));
答案 1 :(得分:-1)