请帮帮我弄清楚,为什么这些代码行在我运行playSpace(FrameLayout)时没有在我的playSpace(FrameLayout)上添加imageView。我只是得到没有图像的playSpace布局。我在做什么错了?
// java代码
包com.code123.en.createObj;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import java.util.Date;
import java.util.Random;
public class MainActivity extends Activity implements View.OnClickListener {
private ViewGroup playSpace;
private float scale;
private Random randomNumber = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playSpace= (ViewGroup)findViewById(R.id.playSpace);
}
private void showAFly()
{
scale = getResources().getDisplayMetrics().density;
int width= playSpace.getWidth();
int height = playSpace.getHeight();
int fly_width = Math.round(scale*50);
int fly_height = Math.round(scale*42);
int left= randomNumber.nextInt(width- fly_width);
int top= randomNumber.nextInt(height- fly_height);
ImageView fly = new ImageView(this);
fly.setImageResource(R.drawable.fly);
fly.setOnClickListener(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(fly_width,fly_height);
params.leftMargin = left;
params.topMargin = top;
params.gravity = Gravity.TOP + Gravity.LEFT;
playSpace.addView(fly,params);
}
@Override
public void onClick(View v) {
doSomething();
}
// activity_main.xml代码行 //手动工作正常
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/playSpace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fly" />
</FrameLayout>
</LinearLayout>
答案 0 :(得分:1)
在将视图添加到ImageView
之前,您需要为Viewgroup
设置布局参数。
fly.setLayoutParams(params);
答案 1 :(得分:0)
您在哪里打电话给showAFly()?
尝试在
之后调用# -*- coding: utf-8 -*-
#import libraries
import urllib.request as urllib2
from bs4 import BeautifulSoup
#specify the url
quote_page = 'https://www.bloomberg.com/quote/SPX:IND'
#query the website and return the html to the variable ‘page’
page = urllib2.urlopen(quote_page)
#parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page, 'html.parser')
#Take out the <div> of name and get its value
name_box = soup.find('h1', attrs={'class': 'companyName'})
name = name_box.text.strip() # strip() is used to remove starting and trailing
print(name)
#get the index price
price_box = soup.find('div', attrs={'class':'price__c3a38e1d'})
price = price_box.text
print(price)