Android移动布局分隔符

时间:2014-11-28 10:55:43

标签: android android-layout android-fragments

嗨伙计们, 我为平板电脑创建了一个简单的应用程序,该应用程序是主/详细信息布局,仅在横向模式下工作。

我在LinearLayout中有两个片段: 第一个片段包含ListView 第二个片段包含TextView

我想在触摸分隔符时移动两个片段之间的分隔符以调整大小/放大,这可能与布局属性o参数或我需要开发代码吗?

我无法添加图片示例,但如果您在横向模式下打开电子邮件应用,则可以在列表电子邮件和所选邮件之间移动分隔符。

我已在横向模式下为Tablet 10.1构建解决方案:

activity_main.xml中:

<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/rootView"
tools:context="com.example.moveview.MainActivity" 
android:animateLayoutChanges="true">

<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:background="@color/red">
</LinearLayout>

 <!--Separator is added dinamic in Activity-->

<LinearLayout
    android:id="@+id/layout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@color/green"
    android:layout_marginLeft="310dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout></RelativeLayout>

值/ color.xml:

<resources>
<color name="red">#d81e1e</color>
<color name="white">#ffffff</color>
<color name="green">#00ab14</color>
<color name="yellow">#FFFF00</color>
<color name="gray">#7A7A7A</color>
<color name="light_blue">#3399FF</color>

MainActivity.java:

    package com.example.moveview;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

    int disWidth;
    int disHeight;

    View separator;
    ViewGroup root_view;
    private int _xDelta;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //SCREEN DIMENSION
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        disWidth =  metrics.widthPixels;
        disHeight = metrics.heightPixels;


        root_view = (ViewGroup)findViewById(R.id.rootView);

        //CREATION VIEW SEPARATOR
        separator = new View(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(10, 700);
        layoutParams.leftMargin = 300;
        layoutParams.topMargin = 0;
        separator.setLayoutParams(layoutParams);
        separator.setBackgroundColor(getResources().getColor(R.color.gray));
        separator.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                drag(event, v);
                return true;
            }
        });
        root_view.addView(separator);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }




    public void drag(MotionEvent event, View view) {

        final int X = (int) event.getRawX();
        LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
        LinearLayout layout3 = (LinearLayout) findViewById(R.id.layout3);

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                view.setBackgroundColor(getResources().getColor(R.color.light_blue));

                _xDelta = X - lParams.leftMargin;
                break;
            case MotionEvent.ACTION_UP:
                view.setBackgroundColor(getResources().getColor(R.color.gray));
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = 0;

                //MIN-MAX MOVE
                if(layoutParams.leftMargin < 50){
                    layoutParams.leftMargin = 50;
                }
                if(layoutParams.leftMargin > disWidth -200){
                    layoutParams.leftMargin = disWidth -200;
                }


                //RIDIMENSION AND ALIGN LEFT/RIGHT VIEW
                RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) layout1.getLayoutParams();
                RelativeLayout.LayoutParams layoutParams3 = (RelativeLayout.LayoutParams) layout3.getLayoutParams();            
                layoutParams1.width = layoutParams.leftMargin;
                layoutParams3.width = disWidth-layoutParams.leftMargin - layoutParams.width;
                layoutParams3.leftMargin = layoutParams.leftMargin + layoutParams.width;
                layout1.setLayoutParams(layoutParams1);
                layout3.setLayoutParams(layoutParams3);

                break;
        }
        root_view.invalidate();
    }

}

1 个答案:

答案 0 :(得分:0)

我认为您需要使用相对布局,但现在我不记得您是否可以使用片段的Relateive布局... 如果您可以更改相对布局的线性布局,请使用以下代码:

 <ListView>
 android:layout_above="@+id/LTextView"
 android:layout_marginBottom="10dp"
 </ListView>

其他快速解决方案你可以在listview和textview之间放置你想要的宽度的其他线性布局,我认为好的模式是(如果第一种模式不起作用),使用这种结构:

<RelativeLayout>
     <FrameLayout>
          <Fragment><ListView></Fragment>
     </FrameLayout>
     <FrameLayout>
          <Fragment><ListView></Fragment>
     </FrameLayout>
</RelativeLayout>

并使用frameLayouts的第一个命令。如果我解决了您的问题或有任何问题建议我!祝你好运Niko80!