layout_weight没有正常工作

时间:2016-09-14 19:58:21

标签: android xml user-interface

我在Android中创建了一个简单的布局,我在显示时遇到了问题。 事实是所有元素都没有显示出来。事实上,只有日历和时间表,但没有其他人。

这是我的代码

<LinearLayout
    android:id="@+id/sms_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:weightSum="8">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/sms_id_sms_pro" />

    <EditText
        android:id="@+id/sms_id_sms_pro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/sms_nb_remainning" />

    <EditText
        android:id="@+id/sms_nb_remainning"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/sms_name_sender" />


    <EditText
        android:id="@+id/sms_name_sender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:maxLength="11" />

    <CalendarView
        android:id="@+id/sms_calendar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TimePicker
        android:id="@+id/sms_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

这就是结果:my result

2 个答案:

答案 0 :(得分:2)

0dp值分配给所有视图的高度,以便可以调整布局权重

android:layout_height="0dp"

查看demo

答案 1 :(得分:0)

我做到了,但结果很奇怪:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jsPDF</title>    
      <script src="jquery.min.js"></script>
      <script src="jspdf.js"></script>
      <script src="FileSaver.js"></script>
      <script src="html2canvas.js"></script>
      <script>
        window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
        function saveFile(){
            var pdf = new jsPDF('p', 'pt', 'a4');
            var source = $('#spendingTable')[0];
            var PDFFILE ='';
            var filename = prompt("Enter a file name:");

            pdf.specialElementHandlers = {
                '#bypassme': function (element, renderer) {
                    return true;
                }
            };

            pdf.fromHTML(source, 15, 15, {'width': 170}, function (dispose) {
                PDFFILE = new Blob([pdf.output()], {type: "application/pdf"});
            });

            //NEXT SAVE IT TO THE DEVICE
            var requestedBytes = 1024*1024*10; // 10MB
            navigator.webkitPersistentStorage.requestQuota (
                requestedBytes, function (grantedBytes) { 
                    window.requestFileSystem(PERSISTENT, 1024*1024, function (fileSystem) {
                        fileSystem.root.getFile(filename +".pdf", {create: true}, function (entry) {
                            var fileEntry = entry;
                            entry.createWriter(function(writer) {
                                writer.onwrite = function(evt) {
                                    alert("Saved to root folder!");
                                };
                                writer.write(PDFFILE);
                            }, 
                            function (error) {
                                alert(error);
                            });
                        }, 
                        function (error) {
                            alert(error);
                        });
                    },
                    function (event) {
                        alert(event.target.error.code);
                    });
                }
            );
        }
      </script>
    </head>
    <body>
    <h1>JSPDF EXAMPLE</h1>
      <hr>
      <div contenteditable="true" id="spendingTable" style="border: 1px black solid">This text is saved into a PDF.</div>
      <br>
      <input type="button" value="Save" onclick="saveFile();">
    </body>

new result