/*This is my mainActivity.class*/
public class MainActivity extends ActionBarActivity{
attachFragments();
}
private void attachFragments() {
CustomFragment chatFragment=new CustomFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.first, chatFragment);
transaction.commit();
}
}
/*mainActivity.xml*/
<?xml version="1.0" encoding="utf-8"?>
< com.prospus.poms.layout.CustomSlidingLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</RelativeLayout>
</com.prospus.poms.layout.CustomSlidingLayout>
CustomSlidingLayout.java
public class CustomSlidingLayout extends LinearLayout {
private static final int SLIDING_DURATION = 500;
private static final int QUERY_INTERVAL = 16;
int mainLayoutWidth;
private View menuRight;
private View content;
private enum MenuState {
HIDING, HIDDEN, SHOWING, SHOWN,
};
private int contentXOffset;
private int menuWidth;
private int rightMenuOffset;
private MenuState currentMenuState = MenuState.HIDDEN;
private Scroller menuScroller = new Scroller(this.getContext(),
new EaseInInterpolator());
private Runnable rightMenuRunnable = new MenuRightRunnable();
private Handler menuHandler = new Handler();
public CustomSlidingLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSlidingLayout(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
menuWidth=mainLayoutWidth;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
content = this.getChildAt(0);
menuRight = getChildAt(1);
menuRight.setVisibility(View.GONE);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
if (changed) {
LayoutParams contentLayoutParams = (LayoutParams) content
.getLayoutParams();
contentLayoutParams.height = this.getHeight();
contentLayoutParams.width = this.getWidth();
LayoutParams menuLayoutParams = (LayoutParams) menuRight.getLayoutParams();
menuLayoutParams.height = this.getHeight();
menuLayoutParams.width = this.getWidth();
}
menuRight.layout(right - rightMenuOffset, top, right - rightMenuOffset
+ menuWidth, bottom);
content.layout(left, top, right, bottom);
}
@SuppressLint("NewApi")
public void toggleRightMenu() {
if (currentMenuState == MenuState.HIDING
|| currentMenuState == MenuState.SHOWING)
return;
switch (currentMenuState) {
case HIDDEN:
currentMenuState = MenuState.SHOWING;
menuRight.setVisibility(View.VISIBLE);
menuScroller.startScroll(0, 0,
-menuRight.getLayoutParams().width, 0, SLIDING_DURATION);
contentXOffset = 0;
rightMenuOffset = 0;
invalidate();
break;
case SHOWN:
currentMenuState = MenuState.HIDING;
menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0,
SLIDING_DURATION);
break;
default:
break;
}
menuHandler.postDelayed(rightMenuRunnable, QUERY_INTERVAL);
this.invalidate();
}
protected class MenuRightRunnable implements Runnable {
@Override
public void run() {
boolean isScrolling = menuScroller.computeScrollOffset();
adjustRightContentPosition(isScrolling);
}
}
@SuppressLint("NewApi")
private void adjustRightContentPosition(boolean isScrolling) {
int scrollerXOffset = menuScroller.getCurrX();
menuRight.offsetLeftAndRight(scrollerXOffset - contentXOffset);
rightMenuOffset += (contentXOffset-scrollerXOffset );
contentXOffset = scrollerXOffset;
this.invalidate();
if (isScrolling)
menuHandler.postDelayed(rightMenuRunnable, QUERY_INTERVAL);
else
this.onMenuSlidingComplete();
}
private void onMenuSlidingComplete() {
switch (currentMenuState) {
case SHOWING:
currentMenuState = MenuState.SHOWN;
break;
case HIDING:
currentMenuState = MenuState.HIDDEN;
break;
default:
return;
}
}
protected class EaseInInterpolator implements Interpolator {
@Override
public float getInterpolation(float t) {
return (float) Math.pow(t - 1, 5) + 1;
}
}
public boolean isMenuShown() {
return currentMenuState == MenuState.SHOWN;
}
}
/*I am using this customlayout in main activity and in each sub layout attching a fragment. */
/* Layout file of fragment: */
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="@layout/header_layout" />
</LinearLayout>
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomLayout"
android:layout_below="@id/title_layout"
android:orientation="horizontal" >
<ListView
android:id="@+id/chatList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:divider="@android:color/black"
android:fadeScrollbars="false"
android:fadingEdge="none"
android:isScrollContainer="false"
android:listSelector="@android:color/transparent"
android:overScrollMode="never"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/title_background"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/upload"
android:layout_width="@dimen/BASE_50_DP"
android:layout_height="match_parent"
android:layout_gravity="bottom" >
<ImageView
android:id="@+id/upload_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
<EditText
android:id="@+id/chattext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="@dimen/BASE_5_DP"
android:layout_marginTop="@dimen/BASE_5_DP"
android:layout_weight="1"
android:background="@android:color/white"
android:maxHeight="@dimen/EditTextHeight"
android:minHeight="@dimen/BASE_45_DP"
android:padding="@dimen/BASE_5_DP"
android:selectAllOnFocus="true"
android:singleLine="false" />
<RelativeLayout
android:layout_width="@dimen/BASE_50_DP"
android:layout_height="match_parent"
android:layout_gravity="bottom" >
<ImageView
android:id="@+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
kayboard隐藏了editText,但是当键盘隐藏时,editText会出现在屏幕中间。与要求完全相反。我知道主要问题是我的自定义布局。请告诉我这个customlayout有什么问题。如果我用relativeLayoot替换customLayout,一切正常。但我必须使用这种布局。请帮我。
答案 0 :(得分:1)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="@layout/header_layout" />
</LinearLayout>
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomLayout"
android:layout_below="@id/title_layout"
android:orientation="horizontal" >
<ListView
android:id="@+id/chatList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:divider="@android:color/black"
android:fadeScrollbars="false"
android:fadingEdge="none"
android:listSelector="@android:color/transparent"
android:overScrollMode="never"
android:scrollbarStyle="insideInset"
android:scrollbarThumbVertical="@drawable/scrollbar_thumb"
android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"
android:scrollbars="vertical"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/title_background"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/upload"
android:layout_width="@dimen/BASE_50_DP"
android:layout_height="match_parent"
android:layout_gravity="bottom" >
<ImageView
android:id="@+id/upload_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/upload_icon" />
</RelativeLayout>
<EditText
android:id="@+id/chattext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="@dimen/BASE_5_DP"
android:layout_marginTop="@dimen/BASE_5_DP"
android:layout_weight="1"
android:background="@android:color/white"
android:maxHeight="@dimen/EditTextHeight"
android:minHeight="@dimen/BASE_45_DP"
android:padding="@dimen/BASE_5_DP"
android:selectAllOnFocus="true"
android:singleLine="false" />
<RelativeLayout
android:layout_width="@dimen/BASE_50_DP"
android:layout_height="match_parent"
android:layout_gravity="bottom" >
<ImageView
android:id="@+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/input_done_icon" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
答案 1 :(得分:0)
将所有视图都设为scrollview
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="@layout/header_layout" />
</LinearLayout>
.....
</RelativeLayout>
</ScrollView>
答案 2 :(得分:0)
将整个相对布局包装在ScrollView内。
也
请注意,scrollview只能有1个子视图
这将是你最外层的相关布局。
以上链接指向scrollview方法的文档,可用于在java文件中设置不同的属性。