2010-03-06 16 views
27

मैं एंड्रॉइड के भयानक लेआउट सिस्टम से जूझ रहा हूं। मैं स्क्रीन भरने के लिए एक टेबल प्राप्त करने की कोशिश कर रहा हूं (सरल दाएं?) लेकिन यह हास्यास्पद रूप से कठिन है।स्क्रीन भरने के लिए मुझे एंड्रॉइड टेबललाउट कैसे मिल सकता है?

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent"> 
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"> 
<Button android:text="A" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> 
<Button android:text="B" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> 
</TableRow> 
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"> 
<Button android:text="C" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> 
<Button android:text="D" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> 
</TableRow> 

हालांकि मैं यह जावा में काम करने के लिए नहीं मिल सकता है:

मैं इसे इस तरह एक्सएमएल में किसी भी तरह काम करने के लिए मिला है। मैंने लेआउटपैम के दस लाख संयोजनों की कोशिश की है, लेकिन कुछ भी काम नहीं करता है।

table = new TableLayout(this); 
    // Java. You suck. 
    TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, 
            ViewGroup.LayoutParams.FILL_PARENT); 
    table.setLayoutParams(lp); // This line has no effect! WHYYYY?! 
    table.setStretchAllColumns(true); 
    for (int r = 0; r < 2; ++r) 
    { 
     TableRow row = new TableRow(this); 
     for (int c = 0; c < 2; ++c) 
     { 
      Button btn = new Button(this); 
      btn.setText("A"); 
      row.addView(btn); 
     } 
     table.addView(row); 
    } 

जाहिर Android दस्तावेज़ कोई मदद नहीं है: यह सबसे अच्छा परिणाम रहा है जो केवल स्क्रीन की चौड़ाई, ऊंचाई नहीं भरता है। क्या किसी के भी पास कोई सुझाव है?

+3

मुझे नहीं लगता कि आप बहुत से लोगों को अपने प्रश्न में उस नकारात्मकता के साथ उत्तर देने के लिए दौड़ने जा रहे हैं। –

+3

हाँ मुझे पता है। जब आप उम्र के लिए संघर्ष करते हैं तो यह वास्तव में निराशाजनक होता है कि एक साधारण कार्य क्या होना चाहिए।'एंड्रॉइड के मंद लेआउट सिस्टम' – Timmmm

+5

+1 यह नहीं है - मैंने कोशिश की है (कई तरीकों से!)। इसके अतिरिक्त एंड्रॉइड डॉक्स में यह कहता है: "टेबललेआउट के बच्चे लेआउट_विड्थ विशेषता निर्दिष्ट नहीं कर सकते हैं। चौड़ाई हमेशा FILL_PARENT होती है। हालांकि, लेआउट_हेइट विशेषता को बच्चे द्वारा परिभाषित किया जा सकता है; डिफ़ॉल्ट मान WRAP_CONTENT है। यदि बच्चा एक टेबलरो है , तो ऊंचाई हमेशा WRAP_CONTENT है। " और "तालिका तालिका के बच्चों को XML फ़ाइल में layout_width और layout_height विशेषताओं को निर्दिष्ट करने की आवश्यकता नहीं है। TableRow हमेशा उन मानों को लागू करता है जो क्रमशः FILL_PARENT और WRAP_CONTENT हो।" –

उत्तर

8

अंत में बाहर काम किया ऐसा करने के तरीके देखें। TableLayout पर दिया गया है और केवल लंबवत एक के अंदर क्षैतिज LinearLayout का उपयोग किया। वजन निर्धारित करने के लिए महत्वपूर्ण कुंजी है। यदि आप FILL_PARENT निर्दिष्ट करते हैं लेकिन डिफ़ॉल्ट वजन के साथ यह काम नहीं करता है:

LinearLayout buttonsView = new LinearLayout(this); 
    buttonsView.setOrientation(LinearLayout.VERTICAL); 
    for (int r = 0; r < 6; ++r) 
    { 
     LinearLayout row = new LinearLayout(this); 
     row.setOrientation(LinearLayout.HORIZONTAL); 
     for (int c = 0; c < 4; ++c) 
     { 
      Button btn = new Button(this); 
      btn.setText("A"); 
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 
      lp.weight = 1.0f; 
      row.addView(btn, lp); 
     } 
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 
     lp.weight = 1.0f; 
     buttonsView.addView(row, lp); 
    } 

    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); 
    setContentView(buttonsView, lp); 
0

आपने कभी भी पंक्ति या बटन लेआउट पैरामीटर सेट नहीं किया है, जबकि पोस्ट किए गए एक्सएमएल में आप ऐसा करते हैं .. लूप के विवरण को पंक्ति लेआउट पैरामीटर और बटन लेआउट पैरामीटर दोनों को सेट करने के लिए इसके परिणाम के रूप में परिणाम देना चाहिए आपका एक्सएमएल

+0

के लिए – Timmmm

34

उपर्युक्त चर्चा में 2 गलतियां हैं।

  1. यह संभव है प्रोग्राम के TableLayout.LayoutParams निर्दिष्ट करने और TableRow.LayoutParams और जैसे उचित निर्माता का उपयोग करके वजन स्थापित करने के लिए

    टेबललेआउट। लेआउट पैराम्स rowInTableLp = नया tableLayout.LayoutParams (लेआउट पैराम्स.फिल_PARENT, लेआउट पैराम्स.फिल_PARENT, 1.0f);

  2. एक विजेट में अपने माता-पिता का लेआउट पैराम होना चाहिए। इसलिए, पंक्तियों TableLayout.LayoutParams

का उपयोग करना चाहिए इससे आप अपने प्रारंभिक कोड की निम्न कार्यरत वर्शन देता है: the solution के लिए Android डेवलपर के मंच पर रोमेन लड़का की टिप्पणी करने के लिए

TableLayout table = new TableLayout(this); 
// Java. You succeed! 
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
     ViewGroup.LayoutParams.FILL_PARENT, 
     ViewGroup.LayoutParams.FILL_PARENT); 
table.setLayoutParams(lp); 
table.setStretchAllColumns(true); 

TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
     ViewGroup.LayoutParams.FILL_PARENT, 
     ViewGroup.LayoutParams.FILL_PARENT, 
     1.0f); 
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
     ViewGroup.LayoutParams.FILL_PARENT, 
     ViewGroup.LayoutParams.FILL_PARENT, 
     1.0f); 
for (int r = 0; r < 2; ++r) 
{ 
    TableRow row = new TableRow(this); 
    for (int c = 0; c < 2; ++c) 
    { 
     Button btn = new Button(this); 
     btn.setText("A"); 
     row.addView(btn, cellLp); 
    } 
    table.addView(row, rowLp); 
} 
setContentView(table); 

धन्यवाद।

+0

व्हाउहा, उम्र के लिए अन्य उदाहरणों के साथ संघर्ष कर रहा है! अंत में यह एक काम करता है! Thaaanks! – bk138

+2

इस वाक्य ने मेरे लिए चाल बनाई: 'एक विजेट में अपने माता-पिता के लेआउट पैराम होना चाहिए। इसलिए, पंक्तियों को TableLayout.LayoutParams ' – hhh3112

+1

का उपयोग करना चाहिए जब तक मैंने इसे पढ़ नहीं लिया है ... ** "एक विजेट में अपने माता-पिता का लेआउट पैराम होना चाहिए।" ** धन्यवाद आपने मुझे बहुत समय बचाया ! – Bojan

0

टेबललेआउट लेआउट पैराम्स सेट करने के लिए, हम तार्किक रूप से टेबललेआउट। लेआउट पैराम्स क्लास का उपयोग करने की उम्मीद करते हैं, लेकिन आपको टेबललेआउट बताते हुए एक कास्ट त्रुटि मिल जाएगी। LayoutParams को फ्रेमलेआउट में लाया नहीं जा सकता। LayoutParams।

तो, यदि आप प्रोग्रामलेटिक रूप से TableLayout गुण सेट करना चाहते हैं, तो आपको फ्रेमलेआउट। लेआउट पैराम का उपयोग करना चाहिए। उदाहरण के लिए:

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT); 
      layoutParams.setMargins(80, 0, 0, 0); 
      TableLayout tableLayout = (TableLayout) findViewById(R.id.header_detail); 
      tableLayout.setLayoutParams(layoutParams); 
संबंधित मुद्दे

 संबंधित मुद्दे