android-重叠视图

android-重叠视图,android,android-layout,android-view,Android,Android Layout,Android View,事情是这样的: 我有一个编辑文本和一个按钮。其概念是让EditText很长,以便它覆盖按钮,但按钮位于顶部。让我们这样想象,[]表示编辑文本宽度,()表示按钮宽度 [ EditText ( Button )] 按钮位于编辑文本的右侧。可能吗? 有什么建议吗?谢谢。您应该创建一个自定义视图。下面的示例中,有一个edittect的botton右键,当您单击它时,它将清除edittext clearable_edit_text.xml <?xml version=”1.0″ en

事情是这样的:

我有一个编辑文本和一个按钮。其概念是让EditText很长,以便它覆盖按钮,但按钮位于顶部。让我们这样想象,[]表示编辑文本宽度,()表示按钮宽度

[ EditText        ( Button )]
按钮位于编辑文本的右侧。可能吗?
有什么建议吗?谢谢。

您应该创建一个自定义视图。下面的示例中,有一个edittect的botton右键,当您单击它时,它将清除edittext

clearable_edit_text.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
>

<EditText
android:id=”@+id/clearable_edit”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:paddingRight=”35dip”
/>
<Button
android:id=”@+id/clearable_button_clear”
android:layout_width=”30dip”
android:layout_height=”30dip”
android:layout_alignParentRight=”true”
android:background=”@drawable/image_clear”
android:layout_centerVertical=”true”
android:layout_marginRight=”5dip”/>

</RelativeLayout>
现在我们定制的视图ClearableEditText已经准备好了,我们可以在任何需要的地方使用它。 像



您为什么要这样做?你知道文本可能会出现在按钮上方,对吗?另外,您尝试过什么?将两者放在一个相对的布局中,然后设置EditText的宽度以填充父对象,并将按钮的android:layout\u alignParentRight设置为true,实际上可以做到这一点。谢谢你,朋友,+1。
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class ClearableEditText extends RelativeLayout
{
LayoutInflater inflater = null;
EditText edit_text;
Button btn_clear;

public ClearableEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initViews();
}

public ClearableEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
initViews();

}

public ClearableEditText(Context context)
{
super(context);
// TODO Auto-generated constructor stub
initViews();
}

void initViews()
{
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.clearable_edit_text, this, true);
edit_text = (EditText) findViewById(R.id.clearable_edit);
btn_clear = (Button) findViewById(R.id.clearable_button_clear);
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
clearText();
showHideClearButton();
}

void clearText()
{
btn_clear.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
edit_text.setText(“”);
}
});
}

void showHideClearButton()
{
edit_text.addTextChangedListener(new TextWatcher()
{

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
if (s.length() > 0)
btn_clear.setVisibility(RelativeLayout.VISIBLE);
else
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub

}
});
}

public Editable getText()
{
Editable text = edit_text.getText();
return text;
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

<com.and.ab1209.ClearableEditText
android:id=”@+id/edit_text_clearable”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />

</LinearLayout>