android scrollview垂直滚动条没有显示出来

时间:2014-04-29 12:05:14

标签: android android-layout android-scrollview

所以我在自定义AlertDialog

的布局下面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popuplayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:fillViewport="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/popup_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:gravity="center"
        android:text="DateTime"
        android:textColor="#cccccc"
        android:textSize="14sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:background="#333333" />

    <ScrollView
        android:id="@+id/popup_scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:orientation="vertical"
        android:padding="10dp" >

        <TextView
            android:id="@+id/popup_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Message Text"
            android:textColor="#FFCC00"
            android:textSize="18sp" />
    </ScrollView>

    <Button
        android:id="@+id/btnSpam"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="markSpam"
        android:padding="10dp"
        android:text="Mark as Spam" />

</LinearLayout>

以下是我如何展示它:

getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(0));

LayoutInflater layoutInflater = LayoutInflater.from(context);

View promptView = layoutInflater.inflate(
    R.layout.activity_show_notification, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
    context);

alertDialogBuilder.setView(promptView);

// create an alert dialog
AlertDialog popup = alertDialogBuilder.create();

popup.setTitle(title);
popup.setMessage("");
popup.show();

TextView messageText = (TextView) popup.findViewById(android.R.id.message);
messageText.setPadding(0, 0, 0, 0);
messageText.setHeight(0);
messageText.setVisibility(View.INVISIBLE);

TextView popupDate = (TextView) popup.findViewById(R.id.popup_date);
TextView popupText = (TextView) popup.findViewById(R.id.popup_text);
popupDate.setText(mdate);
popupText.setText(message);

popupText.setMovementMethod(new ScrollingMovementMethod());

然而,当文本较长时,它会被剪切并且垂直滚动条不显示:(

任何人都可以帮助我在这里失踪吗?对你的帮助表示感谢。感谢

更新

这是app xml文件中使用holo主题进行对话的活动:

 <activity
    android:name="com.domain.notifier.ShowNotification"
    android:label="@string/title_activity_show_notification"
    android:noHistory="true"
    android:theme="@android:style/Theme.Holo.Dialog" >
 </activity>

和styles.xml文件:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.


        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

这是我启动警报对话框活动的方式:

Intent intnt = new Intent(context, ShowNotification.class);
intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intnt.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intnt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

intnt.putExtra("address", address);
intnt.putExtra("body", body);
intnt.putExtra("date", date);
context.startActivity(intnt);

1 个答案:

答案 0 :(得分:2)

我已尝试重现您的问题,并在经过一些修改后运行。基本上我从promptView进行了视图查找。

    getWindow().setBackgroundDrawable(new ColorDrawable(0));

    LayoutInflater layoutInflater = LayoutInflater.from(this);

    View promptView = layoutInflater.inflate(
            R.layout.test, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            this);

    alertDialogBuilder.setView(promptView);

    // create an alert dialog
    AlertDialog popup = alertDialogBuilder.create();
    popup.setTitle("title");

    TextView messageText = (TextView) promptView.findViewById(android.R.id.message);
    TextView popupDate = (TextView) promptView.findViewById(R.id.popup_date);
    TextView popupText = (TextView) promptView.findViewById(R.id.popup_text);

    popupDate.setText("date");
    popupText.setText(longTestString);


    popup.show();

更新:

我忽略了在我的解决方案中,scrollview推出了底部的按钮。 如果在Scrollview中添加布局权重,它可以正常工作。它占用了所有可用空间,并在必要时显示条形图。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popuplayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:fillViewport="true"
android:orientation="vertical" >

<TextView
    android:id="@+id/popup_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dip"
    android:gravity="center"
    android:text="DateTime"
    android:textColor="#cccccc"
    android:textSize="14sp" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:background="#333333" />

<ScrollView
    android:id="@+id/popup_scrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_weight="0.5">

    <TextView
        android:id="@+id/popup_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Message Text"
        android:textColor="#FFCC00"
        android:textSize="18sp" />
</ScrollView>

<Button
    android:id="@+id/btnSpam"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="markSpam"
    android:padding="10dp"
    android:text="Mark as Spam" />

enter image description here