我想自定义AlertDialog
,使其具有固定的页眉和页脚以及可滚动的RecyclerView
。因此,我尝试使用RelativeLayout
进行此操作,但是如果RecyclerView
中有很多项目,则页脚将隐藏在RecyclerView
下。然后,我在RecyclerView
上添加了固定的高度,那么如果项目较少,那么RecyclerView
和页脚之间就会有多余的空间。
使用RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:gravity="center"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/main"
android:orientation="vertical">
<TextView
android:text="Add Additional Information"
android:id="@+id/additionalInfoTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center"
android:textColor="@color/white"
android:textStyle="bold"
android:background="@drawable/popupTitle"
android:padding="@dimen/_10sdp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/additionalDataList"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/drlBottomMenu"
android:layout_height="wrap_content"
android:background="@color/offWhite"
android:orientation="vertical"
android:layout_below="@+id/main">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Continue"
android:layout_gravity="center"
android:id="@+id/continueButton"
style="@style/buttonStyle"
android:backgroundTint="@color/colorPrimary"
android:layout_weight="0.5"
android:textAllCaps="false"
android:padding="12dp"
android:layout_marginTop="@dimen/_12sdp"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
android:textColor="@color/white"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:padding="12dp"
android:layout_gravity="center"
android:id="@+id/cancelButton"
android:textAllCaps="false"
style="@style/buttonStyle"
android:backgroundTint="@color/gray"
android:layout_weight="0.5"
android:textColor="@color/white"
android:layout_marginBottom="@dimen/_12sdp"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
/>
</LinearLayout>
</RelativeLayout>
使用LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/roundedCorner">
<TextView
android:text="Add Additional Information"
android:id="@+id/additionalInfoTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16dp"
android:gravity="center"
android:textColor="@color/white"
android:textStyle="bold"
android:background="@drawable/popupTitle"
android:padding="@dimen/_10sdp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/additionalDataList"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/continueButton"
style="@style/buttonStyle"
android:backgroundTint="@color/colorPrimary"
android:layout_marginTop="@dimen/_12sdp"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
/>
<Button
android:text="Cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cancelButton"
style="@style/buttonStyle"
android:backgroundTint="@color/gray"
android:layout_marginBottom="@dimen/_12sdp"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
/>
</LinearLayout>
答案 0 :(得分:3)
尝试一下
活动代码
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView additionalDataList;
ArrayList<String> arrayList = new ArrayList<>();
DataAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.test);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
additionalDataList = dialog.findViewById(R.id.additionalDataList);
additionalDataList.setLayoutManager(new LinearLayoutManager(this));
additionalDataList.setHasFixedSize(true);
addDataToList();
adapter = new DataAdapter(this, arrayList);
additionalDataList.setAdapter(adapter);
dialog.show();
}
private void addDataToList() {
for (int i = 0; i < 50; i++) {
arrayList.add("Item :" + i);
}
}
}
layout.test
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/additionalInfoTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:gravity="center"
android:padding="10dp"
android:text="Add Additional Information"
android:textColor="@android:color/white"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/additionalDataList"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/additionalDataList"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@+id/drlBottomMenu"
app:layout_constraintEnd_toEndOf="@id/drlBottomMenu"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/additionalInfoTitle" />
<LinearLayout
android:id="@+id/drlBottomMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/additionalDataList">
<Button
android:id="@+id/continueButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:layout_marginRight="20dp"
android:layout_weight="0.5"
android:padding="12dp"
android:text="Continue"
android:textAllCaps="false"
android:textColor="@android:color/white"
app:backgroundTint="@color/colorPrimary" />
<Button
android:id="@+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:layout_marginRight="20dp"
android:layout_weight="0.5"
android:padding="12dp"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="@android:color/white"
app:backgroundTint="@android:color/darker_gray" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
输出
答案 1 :(得分:1)
尝试使用MaterialDialog
或AlertDialog
,然后使用constrainlayout
+ guidelines
创建布局。这样更容易。
答案 2 :(得分:0)
<android.support.v7.widget.RecyclerView
android:id="@+id/additionalDataList"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="height of bottom header"<-------------add this line
/>
或者您可以使用constraintlayout并将高度设置为0dp,以便recylerview 可以占用可用空间
另一种选择是使用线性布局并将其设置为包装内容,然后 动态增加视图
答案 3 :(得分:0)
尝试在约束之类的内部使用Constraint
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.support.constraint.ConstraintLayout
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:padding="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/additionalInfoTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:background="@android:color/darker_gray"
android:gravity="center"
android:padding="10dp"
android:text="Add Additional Information"
android:textColor="@android:color/white"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/additionalDataList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="8"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/additionalInfoTitle" />
<Button
android:id="@+id/continueButton"
style="@style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:backgroundTint="@color/colorPrimary"
android:text="Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/additionalDataList" />
<Button
android:id="@+id/cancelButton"
style="@style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:backgroundTint="@color/gray"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/continueButton" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>