2017-04-12 29 views
0

在您將我標記爲重複項並開始downvotes滾動之前,請知道我正在盡我所能在這裏完成這項工作將近3個小時。我已經嘗試了四種不同的方法,我在Docs和各種論壇主題上閱讀過。如何在Android中正確模板化/ xml重用

我有Button我在獨立xml文件看起來像這樣定義的:

<!--button_template.xml--> 
<Button 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/score_question_btn" 
    android:onClick="viewScore" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="16dp" 
    android:layout_marginLeft="16dp" 
    android:layout_marginRight="16dp" 
    android:padding="24dp" /> 

我想用它來動態填充視圖。

我迄今爲止嘗試:

  • context.findViewById(R.id.button_id);這是不行的,因爲按鈕是不是當前上下文根視圖的子視圖,從而返回null
  • LayoutInflator - >inflate(R.layout.button_template.xml, rootView, false);我可以」不要使用它,因爲我需要爲特定的按鈕設置不同的文本和背景顏色。
  • style resource使用自定義,定義margins,但我無法找到一個方法來設置Button style
  • Button button = new Button(context)簡單地說,我不能得到這個工作。我創建了Button,我可以很容易地設置文本和顏色,但是然後有一個margins的問題。

後半小時試圖穿上Button嚇壞margin,我想出了這個:

LinearLayout.LayoutParams params = 
     new LinearLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
int dpMargin = 16; 
float d = context.getResources().getDisplayMetrics().density; 
int marginInPixels = (int) (dpMargin * d); 
params.setMargins(marginInPixels, marginInPixels, marginInPixels, 0); 

但是,我不知道是否可行,因爲這是一貫崩潰我的項目。它執行一次,崩潰,然後我無法啓動我的項目,因爲它找不到我的MainActivity class。我也花了一個小時跟蹤這個。我想出的唯一修復方法是將我的src文件夾複製到新項目中。

所以關於這個問題:我在正確的軌道上?如果是這樣我做錯了什麼?如果不是的話 - 一個有經驗的android開發人員將如何處理這個模板問題。

+0

「因爲我需要爲特定按鈕設置不同的文本和背景顏色」 - 您可以調用Button實例上的Java方法來設置此信息。 – CommonsWare

+1

我不確定我是否理解你想要達到的目標......你說「我想用它來動態地填充一個視圖。」我想你正在嘗試創建一個按鈕佈局xml,並在應用程序的其他部分/其他佈局,一些線性佈局等內部重用該按鈕? – ramden

回答

1

extend按鈕類..

下面是一個例子

MyReusableButton.java

package com.example.test; 

import android.content.Context; 
import android.graphics.Color; 
import android.util.AttributeSet; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class MyReusableButton extends Button { 

    //use this constructor for button creation from java code 
    public MyReusableButton(Context context) { 
     super(context); 
     init(); 
    } 

    //this is needed for XML inflation 
    public MyReusableButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    //set button style 
    private void init() { 
     setBackgroundColor(Color.RED); 
     setTextColor(Color.WHITE); 
    } 

    //helper to set margins 
    public void setMargins(int left, int top, int right, int bottom) { 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     params.setMargins(left, top, right, bottom); 
     this.setLayoutParams(params); 
    } 
} 

MainActivity.java

package com.example.test; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //use this to create a button in code 
     MyReusableButton b = new MyReusableButton(this); 
     b.setText("Hello, World"); 

     //use this to add margins to the button 
     b.setMargins(10, 10, 10, 10); 

     //add the button to the parent linear layout 
     ((LinearLayout) findViewById(R.id.wrapper)).addView(b); 
    } 
} 

activity_main.xml中

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

    <!-- We can also define a button in XML --> 
    <com.example.test.MyReusableButton 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="Test2" 
     /> 
</LinearLayout> 

此代碼的結果是2個按鈕,一個來自XML,另一個來自程序化創建。 init()方法爲我們提供了每個按鈕所需的樣式。

我還包括一個輔助方法來設置將來保存代碼的邊距。

+0

謝謝。我現在就試一試。這些邊距不是純像素。如果是這樣,你將如何讓他們的DP? – Alex

+0

它的工作,非常感謝。 – Alex

+0

沒有問題,有一個轉換爲dp,但我不知道是否離開我的頭頂,我確信它已經在某處回答了 – Zach