我创建了以下布局。
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TableRow>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Post As" />
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ddlPostAs"
/>
</TableRow>
<TableRow>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Expires In" />
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ddlExpiryOn"
/>
</TableRow>
</TableLayout>
其中android微调器根据数据改变其宽度。 我想阻止它。
任何人都可以指导我如何实现这个目标吗?
任何帮助都会得到满足。
答案 0 :(得分:5)
根据屏幕宽度固定宽度。
DisplayMetrics d = new DisplayMetrics();
getWindow().getWindowManager().getDefaultDisplay().getMetrics(d);
int wdt = d.widthPixels;
ddlpostas.getLayoutParams().width = wdt - 120;
ddlexpiryon.getLayoutParams().width = wdt - 120;
答案 1 :(得分:2)
您可以在import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.table.DefaultTableModel;
public class GUIMain{
private JFrame frmKeyoperateTestframework;
public JTable tableBefore;
public JTable tableMainTest;
public JTable tableAfter;
public JTabbedPane tabbedPane;
public JScrollPane scrollTables;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUIMain window = new GUIMain();
window.frmKeyoperateTestframework.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUIMain() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmKeyoperateTestframework = new JFrame();
frmKeyoperateTestframework.setResizable(false);
frmKeyoperateTestframework.getContentPane().setBackground(Color.WHITE);
frmKeyoperateTestframework.setFont(new Font("Carlito", Font.PLAIN, 14));
frmKeyoperateTestframework.setTitle("GUI");
frmKeyoperateTestframework.setBounds(100, 100, 865, 584);
frmKeyoperateTestframework.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmKeyoperateTestframework.getContentPane().setLayout(null);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
tabbedPane.setFont(new Font("Carlito", Font.PLAIN, 13));
tabbedPane.setBackground(Color.WHITE);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
scrollTables = new JScrollPane();
scrollTables.setBounds(446, 50, 397, 490);
scrollTables.setViewportView(tabbedPane);
frmKeyoperateTestframework.getContentPane().add(scrollTables);
// "5" to show, that only data rows are displayed
DefaultTableModel tableModel = new DefaultTableModel(new String[]{"Client", "Action", "Location", "Value"}, 5);
tableBefore = new JTable();
tableBefore.setName("tableBefore");
tableBefore.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableBefore.setModel(tableModel);
tabbedPane.addTab("Before", null, tableBefore, null);
tableBefore.setFont(new Font("Carlito", Font.PLAIN, 13));
tableBefore.setFocusable(false);
tableMainTest = new JTable();
tableMainTest.setName("tableMainTest");
tableMainTest.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableMainTest.setFont(new Font("Carlito", Font.PLAIN, 13));
tableMainTest.setFocusable(false);
tableMainTest.setModel(tableModel);
tabbedPane.addTab("Main", null, tableMainTest, null);
tabbedPane.setEnabledAt(1, true);
tableAfter = new JTable();
tableAfter.setName("tableAfter");
tableAfter.setModel(tableModel);
tableAfter.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tabbedPane.addTab("After", null, tableAfter, null);
tableAfter.setFont(new Font("Carlito", Font.PLAIN, 13));
tableAfter.setFocusable(false);
tabbedPane.setSelectedComponent(tableMainTest);
}
}
上使用“android:weightSum”来调整屏幕上固定的宽度。
示例:
<TableRow>
通过此代码,您的微调器宽度将设置为TableRow宽度的一半。
根据您的要求更改android:weightSum和android:layout_weight的值。