我创建了像whatsapp一样聊天的Android应用程序。但我想在指定位置展示广告,广告的内容如标题,链接,图片将使用php文件获取。我怎么能这样做。如果你知道,请帮助我。我搜索了很多但没有找到解决方案。
答案 0 :(得分:1)
我通过创建弹出窗口解决了这个问题...我创建了包含TextView,ImgaView,Button等的小条状布局。 popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/popup"
android:color="#66FF0000"
android:layout_height="wrap_content"
android:background="#fff"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:textColor="#fff"
android:text="Ad Description" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/close"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_toRightOf="@+id/textView1"
android:textColor="#fff"
android:text="www.nspiresfot.com" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#fff"
android:layout_toRightOf="@+id/imageView1"
android:text="Ad Title" />
<Button
android:id="@+id/btn_openAd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_toLeftOf="@+id/button1"
android:background="@drawable/send" />
MainActivity.java
package com.popwindow;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
Point p;
PopupWindow popup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_show = (Button) findViewById(R.id.show_popup);
p = new Point();
p.x = 0;
p.y =60;
//Open popup window
new Handler().postDelayed(new Runnable() {
public void run() {
if (p != null)
showPopup(MainActivity.this, p);
}
}, 900L);
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 240;
int popupHeight = 50;
//Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
layout.getBackground().setAlpha(90);
//Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(false);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// popup.showAtLocation(layout, Gravity.NO_GRAVITY, getRequestedOrientation(), getRequestedOrientation());
// Getting a reference to Close button, and close the popup when clicked.
Button close = (Button) layout.findViewById(R.id.button1);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
popup.dismiss();
}
}
然后我使用php文件加载所有textview和imageview。