我有一個全屏對話框片段,包含一個微調器和一個Tablelayout。 我可以通過選擇Spinner的選項來添加元素到tablelayout。這些元素是cardview,充氣到桌面佈局(讓我們不爭論爲什麼我選擇這個,而不是回收站或列表視圖) 我有兩個問題 第一: 視圖不匹配其父寬度(有一個每個元素右側的空格)鍵盤重疊編輯桌面佈局上的文本
第二個: tablelayout的每個元素都有一個Edit文本字段。如果我添加了幾個元素,鍵盤會覆蓋最後一個edittext。 我該如何解決這些問題。我beggining安卓
這裏是我的代碼 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>
**
刪除
android:layout_weight="1"
對於空格,你自己設定的寬度'的android:layout_width = 「150dp」'。嘗試可能將父母和孩子的佈局設置爲'match_parent' – Distwo對於你的問題#2,我不確定自己是否理解得很好,但聽起來你可能想看看這個答案:http://stackoverflow.com/a/10097160/1245894 – Distwo
在AndroidManifest文件中把這行android:windowSoftInputMode =「adjustPan」作爲活動的屬性 –