1

I am creating my own UI component class by extending layout class. Now instead of adding my custom attributes like height , width etc in string xml resources as separate xml, I want to define in Activity class itself. I don't want to add in xml and then refer it from R class in my code. Is there any way to define and access attributes in code itself?

4

1 回答 1

0

这是来自 Pro Android 4 的代码示例:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
public class MainActivity extends Activity 
{ 
private LinearLayout nameContainer; 

private LinearLayout addressContainer; 

private LinearLayout parentContainer; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    createNameContainer(); 

    createAddressContainer(); 

    createParentContainer(); 

    setContentView(parentContainer); 
} 

private void createNameContainer() 
{ 
    nameContainer = new LinearLayout(this); 

    nameContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.WRAP_CONTENT)); 
    nameContainer.setOrientation(LinearLayout.HORIZONTAL); 

    TextView nameLbl = new TextView(this); 
    nameLbl.setText("Name: "); 

    TextView nameValue = new TextView(this);
    nameValue.setText("John Doe"); 

    nameContainer.addView(nameLbl); 
    nameContainer.addView(nameValue); 
} 

private void createAddressContainer() 
{ 
    addressContainer = new LinearLayout(this); 

    addressContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.WRAP_CONTENT)); 
    addressContainer.setOrientation(LinearLayout.VERTICAL); 

    TextView addrLbl = new TextView(this); 
    addrLbl.setText("Address:"); 

    TextView addrValue = new TextView(this); 
    addrValue.setText("911 Hollywood Blvd"); 

    addressContainer.addView(addrLbl); 
    addressContainer.addView(addrValue); 
} 

private void createParentContainer() 
{ 
    parentContainer = new LinearLayout(this); 

    parentContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.FILL_PARENT)); 
    parentContainer.setOrientation(LinearLayout.VERTICAL); 

    parentContainer.addView(nameContainer); 
    parentContainer.addView(addressContainer); 
} 
}

另请参阅这些以获得更多功能:

布局参数 线性布局

ViewGroup 布局参数

StackOverflow 设置布局参数

于 2012-06-07T10:13:11.453 回答