我正在尝试在片段中实现一个面板,该面板可以从屏幕右侧按下按钮进行扩展。我无法使用导航抽屉,因为我已经有左右两侧的导航抽屉。
这个想法是为了实现这样的目标:
我几乎能够使用SlidingDrawer
窗口小部件(它已被弃用..),唯一的问题是我不知道如何让LinearLayout出现在中间,然后在单击按钮展开SlidingDrawer
时移动。
我有以下代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button handle = (Button) findViewById(R.id.handle);
SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
}
});
drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
}
});
}
}
XML
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.slidingdrawertest.slidingdrawertest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST" />
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="200dp"
android:layout_height="400dp"
android:layout_marginTop="100dp"
android:layout_gravity="end"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="horizontal">
<Button
android:id="@+id/handle"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expand" />
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
您可以像这样编辑XML
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context="com.test.slidingdrawertest.slidingdrawertest.MainActivity">
<LinearLayout
android:id="@+id/left" // this is your linear layout
android:layout_width="match_parent" // that you want to shift left
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/right" // apply this property due to
android:layout_alignParentLeft="true" //which if the width of sliding menu
android:gravity="center" //increase it will automatically compressed
android:background="#aa00aa"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST" />
</LinearLayout>
<LinearLayout
android:id="@+id/right"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:background="#0000aa"
android:layout_height="match_parent">
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="200dp" // you can check by increasing or decreasing the with of this slidingdrawer
android:layout_height="400dp"
android:layout_marginTop="100dp"
android:layout_gravity="end"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="horizontal">
<Button
android:id="@+id/handle"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expand" />
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
</RelativeLayout>
除此之外,如果在线性布局的右侧设置任何布局,最初为GONE
,当您点击按钮时,其可见性更改为INVISIBLE/VISIBLE
,因此它会将线性布局移至左侧。希望有所帮助!
答案 1 :(得分:0)
如果你想要一个小例子,我的解决方案是:(我不知道Avani Nagar的解决方案是否有效):
TextView res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView img = (TextView)findViewById(R.id.second);
res = (TextView)findViewById(R.id.first);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setX(img.getX() - 5);
changew(img.getX());
}
});
}
private void changew(float w){
res.getLayoutParams().width = (int)(res.getX() +w);
res.requestLayout();
res.invalidate();
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<TextView
android:layout_toRightOf="@+id/first"
android:text="bonjourrrrrr"
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@android:color/holo_red_dark" />
<TextView
android:text="salutttttttt"
android:id="@id/first"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@android:color/holo_green_light" />
</RelativeLayout>
答案 2 :(得分:0)
我设法做到了,它实际上并不那么困难,但它有点棘手。这是解决方案:
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/topLayout"
tools:context="com.test.slidingdrawertest.slidingdrawertest.MainActivity">
<LinearLayout
android:id="@+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST" />
</LinearLayout>
<LinearLayout
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="200dp"
android:layout_height="400dp"
android:layout_marginTop="100dp"
android:layout_gravity="end"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="horizontal">
<Button
android:id="@+id/handle"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expand" />
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TESTT" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
</RelativeLayout>
MainActivity.java
中的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button handle = (Button) findViewById(R.id.handle);
SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
final View k = findViewById(R.id.mainLayout);
drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
k.setLayoutParams(params);
}
});
drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
k.setLayoutParams(params);
}
});
}
}
并解释说:我必须给主要主要布局一个id(用XML格式),然后在后面的代码中找到它,然后我必须在抽屉打开或关闭时应用新参数。基本上,我们需要找到父布局并为其设置参数。
现在LinearLayout
能够将它移动到抽屉打开的位置,并且它会回到它在抽屉关闭时的原始位置。下一步是用动画制作这个过程,所以它很流畅,并且不会来回跳转到新的位置。