嗨,我在这个问题上花了好几天时间,但我仍然没有得到确切的解决办法。
在我的布局中,我有“页眉和页脚”.In页眉,我显示我的布局标题,在页脚显示AdView.I,我正在使用LinearLayout。
我的xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/create_job_parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#06506D"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/create_newjob_rlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
....
... 2 buttons and one TextView
</RelativeLayout>
<ScrollView
android:id="@+id/creat_scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/creat_scrollview_child_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp" >
.
.10 edittext boxes
.
.
</LinearLayout>
</ScrollView>
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/create_new_job_activity_adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:loadAdOnCreate="true"
app:adSize="SMART_BANNER"
app:adUnitId="adunitid" >
</com.google.ads.AdView>
</LinearLayout>当键盘打开,如果我在清单中添加"adjustResize“,我的整个布局就会出现在顶部,这意味着我的评论也会出现top.If,我会在清单中添加"adjustPan”,什么都不会发生(底部的字段不会滚动)。
在一些文档中,我会添加滚动视图作为父文档,当键盘opens.But时,也会滚动滚动视图底部的字段,如果我使用滚动视图作为父文档,那么我的评论也会转到down.But,我需要在底部always.How中显示显示视图。
任何人都可以帮我..。
发布于 2014-04-14 07:18:55
首先,定义一个类,它将是您的特殊LinearLayout,如下所示:
public class MyLayout extends LinearLayout {
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLayout(Context context) {
super(context);
}
private OnSoftKeyboardListener onSoftKeyboardListener;
@Override
protected void onMeasure(final int widthMeasureSpec,
final int heightMeasureSpec) {
if (onSoftKeyboardListener != null) {
final int newSpec = MeasureSpec.getSize(heightMeasureSpec);
final int oldSpec = getMeasuredHeight();
if (oldSpec > newSpec) {
onSoftKeyboardListener.onShown();
} else {
onSoftKeyboardListener.onHidden();
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public final void setOnSoftKeyboardListener(
final OnSoftKeyboardListener listener) {
this.onSoftKeyboardListener = listener;
}
public interface OnSoftKeyboardListener {
public void onShown();
public void onHidden();
}}
现在您的xml布局将类似于:
<com.example.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/create_job_parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#06506D"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/create_newjob_rlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
/>
</RelativeLayout>
<ScrollView
android:id="@+id/creat_scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/creat_scrollview_child_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp" >
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
/>
</LinearLayout>
</ScrollView>
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/create_new_job_activity_adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:loadAdOnCreate="true"
app:adSize="SMART_BANNER"
app:adUnitId="adunitid">
</com.google.ads.AdView>
其中com.example是您创建MyLayout类的包名。
现在,在调用xml布局的活动中,请编写以下代码
((MyLayout)findViewById(R.id.create_job_parent_layout)).
setOnSoftKeyboardListener(new OnSoftKeyboardListener() {
@Override
public void onShown() {
findViewById(R.id.create_new_job_activity_adView).
setVisibility(View.GONE);
}
@Override
public void onHidden() {
findViewById(R.id.create_new_job_activity_adView).
setVisibility(View.VISIBLE);
}
});这肯定能解决你的问题..。
干杯..。
发布于 2014-04-14 06:21:03
我建议您在每次EditText视图聚焦时隐藏广告视图。检查是否有人使用View.isFocused()集中精力。如果为真,则使用AdView.setVisibility(View.INVISIBLE);。如果没有一个editText视图是聚焦的,那么使用AdView.setVisibility(View.VISIBLE);显示广告。没有直接的方法来检查键盘是否显示在屏幕上,但有一个解决办法,如检查屏幕大小的变化.
编辑:这是我建议的解决方案.一个监听器,用于检测活动布局的根视图所采取的任何大小更改。计算活动根视图大小与活动窗口视图大小之间的差异。如果它大于150,它很可能是一个键盘。
final View ad = (View) findViewById(R.id.create_new_job_activity_adView);
final View activityRootView = findViewById(R.id.create_job_parent_layout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// calculate the difference between the root view height
// and the window view height
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
// if more than 100 pixels, its probably a keyboard...
if (heightDiff > 150) {
ad.setVisibility(View.INVISIBLE);
Log.i("moayad", "Keyboard is visible");
// keyboard is visible, do something here
} else {
ad.setVisibility(View.VISIBLE);
Log.i("moayad", "Keyboard is invisible");
// keyboard is not visible, do something here
}
}
});发布于 2014-04-14 06:23:16
我在上面的代码中看到了两个错误。您没有在任何地方关闭父LinearLayout,而是关闭了
<RelativeLayout
android:id="@+id/create_newjob_rlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>不加任何内容。你可能错了,把它关错地方了。
https://stackoverflow.com/questions/23052571
复制相似问题