我有一个带有Spinner和Tablelayout的全屏对话框片段。 我可以通过选择Spinner的选项将元素添加到tablelayout。这些元素是卡片视图,被夸大到tablelayout(让我们不要争论为什么我选择这个而不是recyclerview或listview) 我有两个问题 第一: 视图与其父宽度不匹配(每个元素右侧都有一个空格)
第二个: tablelayout的每个元素都有一个Edit文本字段。如果我添加几个元素,键盘与最后的edittexts重叠。 我该如何解决这些问题。我正在开始使用android这是我的代码 的 Dialog_search_contract.java
public class Dialog_search_contract extends DialogFragment {
//private Arraylist<>
private static final String TAG = "AKDialogFragment";
private View view_search;
private TableLayout tabla;
private Dialog_manager dm=Dialog_manager.getDialog_manager();
private TableRow fila;
private final TableRow.LayoutParams layoutFila = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
private final TableRow.LayoutParams layout_celda = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
boolean flags[];
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getActivity()instanceof Comercial) {
view_search = inflater.inflate(R.layout.search_contract_comercial, container, false);
}
else{
view_search = inflater.inflate(R.layout.search_contract, container, false);
}
tabla = (TableLayout) view_search.findViewById(R.id.table_search);
tabla.setLayoutParams(layout_celda);
Toolbar toolbar = (Toolbar) view_search.findViewById(R.id.toolbar_search_contract);
toolbar.setTitle("Buscar contrato");
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
}
setHasOptionsMenu(true);
Spinner opcion = (Spinner) view_search.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(view_search.getContext(), R.array.contract_search,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
opcion.setAdapter(staticAdapter);
opcion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
addCriteria(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view_search;
}
public void addCriteria(int position) {
Filtro_contract aux= getOption(position);
View tr= createView(aux,position);
addTextField(aux,tr);
addRemoveButton(tr);
addSpinner(aux,tr);
addOptionText(aux,tr);
tabla.addView(tr);
}
private View createView(Filtro_contract filter, int position) {
LayoutInflater inflater;
View tr = null;
inflater=LayoutInflater.from(view_search.getContext());
tr = inflater.inflate(R.layout.cardview_search, null,false);
tr.setId(position);
tr.setLayoutParams(layoutFila);
return tr;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.search_contract_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
sendQuery();
return true;
} else if (id == android.R.id.home) {
dismiss();
dm.closeDialog();
Log.d("--","Se ha cerrado el dialogo Dialog_search_contract");
return true;
}
return super.onOptionsItemSelected(item);
}
private void sendQuery() {
Context context=getActivity();
Contract_query_instance.setContract_query_instance(generateQuery());
if (getActivity()instanceof Comercial) {
Intent i = new Intent(context, Result_contract.class);
context.startActivity(i);
}
else{
((Result_contract)getActivity()).load_data();
}
dm.closeDialog();
dismiss();
}
public Contract_query generateQuery()
{ ArrayList<Contract_condition> condiciones= new ArrayList<Contract_condition>();
for (int i = 0; i < tabla.getChildCount(); i++) {
View criterio= tabla.getChildAt(i);
int id=criterio.getId();
Filtro_contract filtro=getOption(id);
Spinner sp=(Spinner)criterio.findViewById(R.id.spinner);
int cond=sp.getSelectedItemPosition();
Compare compare= getCompare(cond,filtro);
EditText txt= (EditText)criterio.findViewById(R.id.texto);
String texto= String.valueOf(txt.getText());
if ((!sonEspacios(texto)&& texto!=null)
|| compare==Compare.NOT_STABLISHED
|| compare==compare.STABLISHED ) {
//Reemplaza varios espacios por uno y elimina los del comienzo y el final
String text = texto.replaceAll(" +", " ").trim();
if (filtro==Filtro_contract.SIGNING_DATE||filtro==Filtro_contract.DUE_DATE)
{
Date fecha= FormatDate.parse_from_dd_MM_yy(text);
text=FormatDate.format_to_yyyy_MM_dd(fecha);
}
condiciones.add(new Contract_condition(filtro, compare, text));
}
}
Contract_query qry= new Contract_query(condiciones);
Log.d("Se ha generado la ","consulta");
return qry;
}
}
search_contract.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@android:color/background_light">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="false" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_search_contract"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Agregar Condición"
android:id="@+id/agregarCondicionText" />
<Spinner android:text="@string/opcion"
android:id="@+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<ScrollView
android:id="@+id/scrollvertical_search_contract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_weight="1">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollhorizontal_search_contract"
android:scrollbars="horizontal"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout android:id="@+id/table_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
cardview_search.xml
**
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="120dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/cardview_header"
android:text="filtro"
android:id="@+id/filtro"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/boton_cerrar"
android:src="@drawable/ic_close_box"
android:layout_alignParentTop="true"
android:background="@null"
android:layout_alignParentRight="true"/>
<Spinner android:text="@string/opcion"
android:id="@+id/spinner"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/linea"
android:layout_marginRight="5dp"
/>
<EditText
android:id="@+id/texto"
android:layout_width="100dp"
android:gravity="end"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_marginEnd="10dp"
android:layout_alignTop="@id/spinner"
android:layout_toRightOf="@id/spinner"
android:layout_marginRight="10dp"
/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="#ffd5d5d5"
android:id="@+id/linea"
android:layout_below="@+id/filtro"
android:layout_marginBottom="5dp"></View>
<!--ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/estado"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
/-->
</RelativeLayout>
</android.support.v7.widget.CardView>
**
答案 0 :(得分:0)
问题1:
尝试将android:layout_width="match_parent"
用于CardView
和RelativeLayout
:
<强> cardview_search.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="120dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/cardview_header"
android:text="filtro"
android:id="@+id/filtro"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/boton_cerrar"
android:src="@drawable/ic_close_box"
android:layout_alignParentTop="true"
android:background="@null"
android:layout_alignParentRight="true"/>
<Spinner android:text="@string/opcion"
android:id="@+id/spinner"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/linea"
android:layout_marginRight="5dp"
/>
<EditText
android:id="@+id/texto"
android:layout_width="100dp"
android:gravity="end"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_marginEnd="10dp"
android:layout_alignTop="@id/spinner"
android:layout_toRightOf="@id/spinner"
android:layout_marginRight="10dp"
/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="#ffd5d5d5"
android:id="@+id/linea"
android:layout_below="@+id/filtro"
android:layout_marginBottom="5dp"></View>
<!--ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/estado"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
/-->
</RelativeLayout>
</android.support.v7.widget.CardView>
问题2:
从android:layout_weight="1"
ScrollView