Как показать один макет поверх другого программным способом в моем случае?
Мой основной макет main.xml просто содержит два LinearLayouts:
- В первом
LinearLayout
размещаются a VideoView
и Button
,
- Второй
LinearLayout
размещает EditText
, и этот LinearLayout
установил значение видимости в значение GONE "(android:visibility="gone"
)
как показано ниже:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/first_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<VideoView
android:id="@+id/my_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
/>
<Button
android:id="@+id/my_btn"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_gravity="right|bottom"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/second_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:visibility="gone"
>
<EditText
android:id="@+id/edit_text_field"
android:layout_height="40dip"
android:layout_width="fill_parent"
android:layout_weight="5"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
</LinearLayout>
Я успешно реализовал функцию, которая при нажатии Button
(с id my_btn) отображается 2 LinearLayout
с полем EditText
со следующим кодом Java:
LinearLayout secondLL = (LinearLayout) findViewById(R.id.second_ll);
Button myBtn = (Button) findViewById(R.id.my_btn);
myBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
int visibility = secondLL.getVisibility();
if(visibility==View.GONE)
secondLL.setVisibility(View.VISIBLE);
}
});
С приведенным выше кодом Java 2 LinearLayout
с EditText
отображается как , добавив ниже 1st LinearLayout
, который имеет смысл.
НО, мне нужно: при нажатии Button
(id: my_btn) отображается 2nd LinearLayout
с EditText
поверх 1st LinearLayout
, который выглядит так: 2nd LinearLayout
с EditText
поднимается снизу экрана, а 2nd LinearLayout
с EditText
только занимают часть экрана снизу, что первый LinearLayout все еще отображается, как показано на рисунке ниже:
![enter image description here]()
Итак, когда нажата Button
(id: my_btn), как показать 2nd LinearLayout
с EditText
поверх 1 LinearLayout
вместо добавления 2nd LinearLayout
ниже 1st LinearLayout
программно?
Ответы
Ответ 1
Используйте FrameLayout с двумя детьми. Двое детей будут перекрываться. Это рекомендуется в одном из руководств от Android на самом деле, это не взлом...
Вот пример, где TextView отображается поверх ImageView:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/golden_gate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</FrameLayout>
![Here is the result]()
Ответ 2
Ответ, данный Александру, работает довольно хорошо. По его словам, важно, чтобы этот "accessor" -view был добавлен как последний элемент. Вот какой код, который помогло:
...
...
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
<!-- place a FrameLayout (match_parent) as the last child -->
<FrameLayout
android:id="@+id/icon_frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</TabHost>
в Java:
final MaterialDialog materialDialog = (MaterialDialog) dialogInterface;
FrameLayout frameLayout = (FrameLayout) materialDialog
.findViewById(R.id.icon_frame_container);
frameLayout.setOnTouchListener(
new OnSwipeTouchListener(ShowCardActivity.this) {