我想为Android View设置以下属性,但是使用代码。
这是我要复制的AdView:
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#000000"
android:paddingTop="5dp"
app:adSize="SMART_BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
这是我到目前为止的代码:
mAdView = new AdView(m_Context);
// Sizes BANNER, SMART_BANNER, LARGE_BANNER
mAdView.setAdSize(AdSize.SMART_BANNER);
//mAdView.setAdUnitId(adIdBanner);
mAdView.setBackgroundColor(0xFF000000); // First byte alpha
mAdView.setPadding(0, 5, 0, 0);
lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mAdView.setLayoutParams(lp);
myLayout.addView(mAdView);
// OR SHOULD IT BE THIS????
//LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
// ViewGroup.LayoutParams.MATCH_PARENT,
// ViewGroup.LayoutParams.MATCH_PARENT);
//myLayout.addView(mAdView, params);
这正确吗?
那么CenterInParent呢?
我是否正确设置了宽度和高度,所以宽度是matchparent,高度是wrapcontent?
答案 0 :(得分:1)
尝试一下:
int paddingTop = 5;
float density = m_Context.getResources().getDisplayMetrics().density
int padding = (int)(paddingTop * density);
mAdView = new AdView(m_Context);
// Sizes BANNER, SMART_BANNER, LARGE_BANNER
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId(adIdBanner);
mAdView.setBackgroundColor(Color.parseColor("#000000"));
mAdView.setPadding(0, padding , 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mAdView.setLayoutParams(params);
myLayout.addView(mAdView);